所以我有以下代码,这是一个带弹出框的简单if语句。我的问题是,当我使用任何(错误== 1)而不是任何(任何(错误== 1))时,为什么这不起作用?
K=100
error = zeros(K,28)
%lots of other code
error(K,2)=1 %this is here as a test to trigger the true logic statement
if any(any(error==1))
disp('hello')
f = figure;
h = uicontrol('Position',[20 20 50 40],'String','Ok','Callback','uiresume(gcbf)');
uiwait(gcf);
close(f);
end
我的代码正在运行,但我想了解"任何"的工作原理。功能
答案 0 :(得分:2)
因为if
声明。
if
语句需要1个逻辑才能继续。如果它有超过1个输入,则需要它们全部为真。
在您的代码中:
>> any(error==1)
ans =
Columns 1 through 18
0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
Columns 19 through 28
0 0 0 0 0 0 0 0 0 0
和
>> any(any(error==1))
ans =
1
因此它是第一种情况,因为向量的所有索引都不为真,它将跳过if,但是在第二种情况下,因为答案是1,它执行代码。
试试这个并亲自看看
if [0 1]
disp('This is not going to be displayed')
end
if [1 1]
disp('Hellooo, this will!')
end
if any([0 1])
disp('Yay! this also!')
end