フィルターのクリア

While loop keep running (logical with vector )

2 ビュー (過去 30 日間)
Ali Tawfik
Ali Tawfik 2020 年 9 月 10 日
コメント済み: Ameer Hamza 2020 年 9 月 10 日
Hi all,
My while loop running without stopping, I wanna it run until reach to logical function of a equal or smaller than 10. which should be any two numbers a=[6 3] or a=[6 6]
thanks,
clearvars;
clc;
a=[0 0];
while all(a)<=6
z=[6 10 8 9 22 3 6];
for i=1:2
z(i)=z(randi(end));
a(i)=z(i)+1
end
end
  2 件のコメント
Ameer Hamza
Ameer Hamza 2020 年 9 月 10 日
Can you describe the condition more clearly?
Ali Tawfik
Ali Tawfik 2020 年 9 月 10 日
Hi Ameer,
Thanks for your prompt reply.
Actually I am trying to run the loop until the following condition meet:
(a) is smaller or equal to 6.

サインインしてコメントする。

採用された回答

Ameer Hamza
Ameer Hamza 2020 年 9 月 10 日
編集済み: Ameer Hamza 2020 年 9 月 10 日
Check this
clc;
a=[inf inf]; % start with all elemets > 6
while ~all(a <= 6)
z=[6 10 8 9 22 3 6];
for i=1:2
z(i)=z(randi(end));
a(i)=z(i)+1
end
end
  4 件のコメント
Ali Tawfik
Ali Tawfik 2020 年 9 月 10 日
@ Ameer, thanks Now its working... Would you mind having a look into my new question as I have been stucked for a while !!! and need any help !
Ameer Hamza
Ameer Hamza 2020 年 9 月 10 日
I am glad to be of help! The other question seems to be a bit complicated to debug, but the main error is in the use of all() function. As you can see in my code, the comparison should be inside the all() function, not outside.
For example, instead of
all(x) > 1
you should write
all(x > 1)

サインインしてコメントする。

その他の回答 (1 件)

BOB MATHEW SYJI
BOB MATHEW SYJI 2020 年 9 月 10 日
I think replacing while with if solves the problem
clearvars;
clc;
a=[0 0];
if all(a)<=6
z=[6 10 8 9 22 3 6];
for i=1:2
z(i)=z(randi(end));
a(i)=z(i)+1
end
end
  1 件のコメント
Ali Tawfik
Ali Tawfik 2020 年 9 月 10 日
Thanks Bob, but I need to use while !!

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by