How to count in nested if statement in matlab?

放肆的年华 提交于 2020-01-17 01:40:14

问题


I want to increment the count and display the value in a message box. I'm using a nested if statement. This is my code

if sum( abs( f1(:) - f2(:))) == 0.0
       i = i + 1;
elseif sum(abs(f2(:) - f3(:))) == 0.0
       i = i+ 1;
elseif sum(abs(f3(:) - f4(:))) == 0.0
       i = i + 1;
else 
       i = 1;   
end

h = msgbox('Perfect  = %d',i);

Here f1,f2,f3, and f4 contains the difference between two images in float. I have declared i = 0; before if statement. Still I'm not able to see the message box in the output. I tried with disp() function too, but its showing only the else part i.e, i = 1

Any suggestions?

Thanks in advance!


回答1:


Each mutually exclusive branch of your decision tree is either i=i+1 or i=1. No matter which one runs, if i was zero before, it will be one afterwards.

I did not understand what you want, but the code as written checks for several conditions and does the same thing no matter what, which can't be right.

Edit: try this

if sum( abs( f1(:) - f2(:))) == 0.0
   i = i + 1;
end
if sum(abs(f2(:) - f3(:))) == 0.0
   i = i+ 1;
end
if sum(abs(f3(:) - f4(:))) == 0.0
   i = i + 1;
end

h = msgbox('Perfect  = %d',i);

This will give you a count of the number of matches, from zero to three. Now all conditions are checked independently, before the second one would only be checked if the first one was false.



来源:https://stackoverflow.com/questions/30244098/how-to-count-in-nested-if-statement-in-matlab

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!