如何在matlab中嵌套if语句计数? [英] How to count in nested if statement in matlab?

查看:1331
本文介绍了如何在matlab中嵌套if语句计数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想增加计数并在消息框中显示该值。我使用嵌套的if语句。这是我的代码

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);

这里 f1 f2 f3 f4 包含两个图像之间的差异。我已经声明 i = 0; 之前的if语句。仍然我无法看到输出中的消息框。我尝试了 disp()函数,但它只显示 else part ie, i = 1

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

有任何建议吗?

>

推荐答案

决策树的每个互斥分支都是 i = i + 1 i = 1 不管是哪一个,如果 i 之前为零,则它将是一个。

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.

编辑:尝试此

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.

这篇关于如何在matlab中嵌套if语句计数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆