user3483746
user3483746

Reputation: 155

How to count in nested if statement in matlab?

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!

Upvotes: 0

Views: 205

Answers (1)

Emilio M Bumachar
Emilio M Bumachar

Reputation: 2613

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.

Upvotes: 1

Related Questions