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

采纳的回答

Guillaume
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 个)

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by