How to count the value in nested if-else statement in matlab

I want to display the count using nested if-else 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 = 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' as '0' before if statement. Still I'm not getting the output.
I have attached the code with the input images.
Any suggestions? Thanks in advance

1 comentario

What do you want to get? Which result are you expecting for i?

Iniciar sesión para comentar.

Respuestas (3)

Are you aware that abs( f1(:) - f2(:)) can only be 0 for any particular element if the two corresponding elements are bitwise identical (and do not represent inf or NaN) ? Which is the same as testing the two using == and wanting non-zero if they are not equal? And so is the same as f1(:) ~= f2(:) ?
And are you aware that the sum of elements that are all non-negative can only be 0 if all of the elements are 0? Which is the same as testing to see if any of them are non-zero and returning false if so? Which is ~any(f1(:) ~= f2(:)) ? And that by De Morgan's Laws, ~any(X) is the same as all(~X) ? And since the inside is a negated condition, what you are testing can therefore be written as
if all(f1(:) == f2(:))
In the situation where f1 and f2 have the same dimensions, this can be abbreviated to
if f1 == f2

1 comentario

Your code reads in several images, does normalized histograms on them, and then checks to see if the histograms are completely identical, with counters "i" and "j" reflecting some combination of exactly which of the histograms are completely identical. Your counters, in the code you give, can only ever be +1 or -1.
Why you are doing that is a mystery. Your code does not contain comments about what the desired output is.
It would make more sense to me if the counters reflected how many locations in the histograms were identical. It would make even more sense to me if you were calculating some kind of "distance" between the histograms, and so calculating a measure of how similar they are rather than whether they are identical or not.

Iniciar sesión para comentar.

well first of all msgbox doesn't work that way. if you need to
i =1
msgtext = sprintf('perfect = %d',i);
h = msgbox(msgtext);
When you say you are not getting output do you mean the msgbox or i not incrementing?

3 comentarios

@Joseph Cheng
Okay! Now I can get the msgbox. But the problem is 'i' is not incrementing or decrementing. The result is 1. Is there any way to achieve the incrementing part?
Joseph Cheng
Joseph Cheng el 14 de Mayo de 2015
Editada: Joseph Cheng el 14 de Mayo de 2015
ok so now i understand what is happening. if else statements doesn't work in the way you are trying. What happens is whatever if statement is met it only does that section and breaks out of the ifstatements.
for example
example = 1;
if example<2
display('if statement 1');
elseif example<3
display('if statement 2');
elseif example<4
display('if statement 3');
end
would only display the "if statement 1" even though the criteria is still met in the elseif statements.
you can just break them off into seperate if statements.
forgot to add that it is only 1 because it is only going into one of the ifstatement not all of them.

Iniciar sesión para comentar.

Jan
Jan el 14 de Mayo de 2015
Do not check for equality of floating point expressions, when rounding errors can influence the results. How large is sum(abs(f3(:) - f4(:)))? it it is in the order or eps consider a certain limit.

3 comentarios

@Jan Simon
Now I edited my code:
i = 0;
j = 0;
if sum( abs( f1(:) - f2(:))) == 0.0
i = i + 1;
else
j = j + 1;
end
if sum(abs(f2(:) - f3(:))) == 0.0
i = i + 1;
else
j = j + 1;
end
if sum(abs(f3(:) - f4(:))) == 0.0
i = i + 1;
else
j = j + 1;
end
I'm able to count the number, but it's taking only the else part. I mean, it's adding up only j variable and not even looking at i variable. Any suggestions?
Since you did not attach a file for f1, f2, f3, and f4 we do not know why the difference ~= 0. as the code only does the else section which counts j they are not equal. If you know they are equal then maybe it is the rounding error that Jan stated. When you perform this in the command window do you get 0?
maybe you can adjust the condition to go
if sum(f1(:) == f2(:)) == numel(f1)
or
if sum(f1(:)~=f2(:))>0
j=j+1;
else
i=i+1;
end
@Joseph Cheng
I have attached the files. Before segmenting the images i have resized to a format of [512,512].

Iniciar sesión para comentar.

Categorías

Más información sobre Creating, Deleting, and Querying Graphics Objects en Centro de ayuda y File Exchange.

Etiquetas

Preguntada:

el 14 de Mayo de 2015

Comentada:

el 15 de Mayo de 2015

Community Treasure Hunt

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

Start Hunting!

Translated by