easy if statement not working
4 次查看(过去 30 天)
显示 更早的评论
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 个评论
Stephen23
2017-6-2
编辑:Stephen23
2017-6-2
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:
采纳的回答
Guillaume
2017-6-2
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 个评论
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Loops and Conditional Statements 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!