MATLAB: Easy if statement not working

for loopif statementloopMATLAB

I am trying to make (what I thought would be) a simple if loop. Here is what I've tried; none work. I have a 384-by-384 matrix called clutter_mask which I need to use to create a same size matrix called dBZ_Mask which follows the given formula if clutter_mask ~= 0, otherwise it stays 0. What am I missing?
dBZ_Mask = clutter_mask * 0.375 + 66;
if dBZ_Mask == 66
dBZ_Mask = 0;
end
%-------------------------


if clutter_mask ~= 0
dBZ_Mask = clutter_mask * 0.375 + 66;
end
%-------------------------
dBZ_Mask(clutter_mask ~= 0) = clutter_mask * 0.375 + 66;
%-------------------------
dBZ_Mask = clutter_mask * 0.375 + 66;
for i=1:384
for j=1:384
if dBZ_Mask(i,j) == 66
dBZ_Mask(i,j)=0;
end
end
end

Best Answer

Look at the result of
dbZ_Mask == 66
Notice that it is a logical array. As stated in the documentation of if: _An expression is true when its result is nonempty and contains only nonzero elements (logical or real numeric). Hence your if will only be true if all the elements of dbZ_Mask are equal to 66.
You either have to use a loop or the proper matlab way which is to not use if at all and use logical indexing instead. In just one line:
dbz_Mask(dbz_Mask == 66) = 0;