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

4 次查看(过去 30 天)
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

回答(3 个)

Walter Roberson
Walter Roberson 2015-5-15
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 个评论
Walter Roberson
Walter Roberson 2015-5-15
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.

请先登录,再进行评论。


Joseph Cheng
Joseph Cheng 2015-5-14
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 个评论
Joseph Cheng
Joseph Cheng 2015-5-14
编辑:Joseph Cheng 2015-5-14
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.
Joseph Cheng
Joseph Cheng 2015-5-14
forgot to add that it is only 1 because it is only going into one of the ifstatement not all of them.

请先登录,再进行评论。


Jan
Jan 2015-5-14
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 个评论
Joseph Cheng
Joseph Cheng 2015-5-15
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
varsha
varsha 2015-5-15
@Joseph Cheng
I have attached the files. Before segmenting the images i have resized to a format of [512,512].

请先登录,再进行评论。

标签

Community Treasure Hunt

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

Start Hunting!

Translated by