easy if statement not working

4 Ansichten (letzte 30 Tage)
katarado
katarado am 2 Jun. 2017
Bearbeitet: Stephen23 am 2 Jun. 2017
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
  1 Kommentar
Stephen23
Stephen23 am 2 Jun. 2017
Bearbeitet: Stephen23 am 2 Jun. 2017
Be careful of comparing floating-point like that. Small differences in the floating-point values means that you should not expect an output equivalent to some mathematical operation/s. Floating-point numbers have been explained a thousand times on this forum:
etc, etc, etc
You might like to actually check the values that you think are whole numbers, and see what values you really have:
fprintf('%.30f\n',dBZ_Mask(i,j))
Or try this FEX submission:

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Guillaume
Guillaume am 2 Jun. 2017
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;

Weitere Antworten (0)

Kategorien

Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange

Produkte

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by