easy if statement not working
4 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
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 Commento
Stephen23
il 2 Giu 2017
Modificato: Stephen23
il 2 Giu 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:
Risposta accettata
Guillaume
il 2 Giu 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;
0 Commenti
Più risposte (0)
Vedere anche
Categorie
Scopri di più su Loops and Conditional Statements in Help Center e File Exchange
Prodotti
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!