while循环在Matlab中循环 [英] While loop inside for loop in Matlab

查看:259
本文介绍了while循环在Matlab中循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在Matlab中的for循环中使用while循环。 while循环将重复相同的操作,直到满足一些标准。 while循环的结果是for循环中的一个迭代。

  n = 100; 
for i = 1:n
while b <0.5
x(i)= rand;
b = x(i);
end
end

我不知道我做错了什么。
谢谢

解决方案

在你展示的例子中,你必须初始化 b ,而 -statement不能在第一次调用时被评估。

/ code> -loop以避免在第一个之后发生误报 -iteration:

  N = 100; 
for ii = 1:n
b = 0;
,而b <0.5
x(ii)= rand;
b = x(ii);
end
end

或者 b

  n = 100; 
x =零(1,100); (ii)<0.5
x(ii)=兰特;(ii)
为ii = 1:n

end
end


I am trying to using a while loop inside a for loop in Matlab. The while loop will repeat the same action until it satifies some criteria. The outcome from the while loop is one iteration in the for loop. I am having a problem to get that correctly.

n=100;
for i=1:n
    while b<0.5
        x(i)=rand;
        b=x(i);
    end
end

I am not sure what i am doing wrongly. Thanks

解决方案

With the example you showed, you have to initialize b or the while-statement cannot be evaluated when it is first called.
Do it inside the for-loop to avoid false positives after the first for-iteration:

n=100;
for ii=1:n
    b = 0;
    while b<0.5
        x(ii)=rand;
        b=x(ii);
    end
end

Or, without b:

n=100;
x = zeros(1,100);
for ii=1:n
    while x(ii)<0.5
        x(ii)=rand;
    end
end

这篇关于while循环在Matlab中循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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