0

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!

user3483746
  • 155
  • 3
  • 4
  • 13
  • 3
    Could be a floating point precision issue - http://stackoverflow.com/questions/686439/why-is-24-0000-not-equal-to-24-0000-in-matlab – Divakar May 14 '15 at 17:59
  • @Divakar I'm using `i` only for counting. Float will not affect that I guess. – user3483746 May 14 '15 at 18:02
  • 3
    .@Divakar is refering to the exact equalities of the sums to `0`. If the `f*` vectors are doubles, exactly equating to `0` is fairly difficult to achieve in floating point in general. – TroyHaskin May 14 '15 at 18:15

1 Answers1

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.

Emilio M Bumachar
  • 2,532
  • 3
  • 26
  • 30
  • Actually I need to display how much if the input is perfect and how much it not matching. So for that I'm using count. Is there any other way to know the perfect and not perfect count. Do I have to change the if statement or something? – user3483746 May 14 '15 at 18:07