How can I fix value of vector inside for loop ?

2 ビュー (過去 30 日間)
omar th
omar th 2023 年 3 月 4 日
コメント済み: Voss 2023 年 3 月 6 日
how can I fixed the value of " select_angles " in case the condition is satisfied ? I tried to fix it via using repeat element function but didn't work.
I appreciate any help.
angles = [ 20 45 60 30 90];
randomize_angles=randperm(numel(angles));
for i=1:numel(randomize_angles)
select_angle = angles(randomize_angles(i))
if condition
select_angle = repelem(pickangle11,1)
end
end
condition = something
  2 件のコメント
Torsten
Torsten 2023 年 3 月 4 日
how can I fixed the value of " select_angles " in case the condition is satisfied ?
Not clear what you mean.
omar th
omar th 2023 年 3 月 4 日
@Torsten thanks for your query, actually I mean how can I fix the value of "select angles which is changing in each iteration of the for loop " due to I have condition when that condition is satisfied I want to stop this select angle and prevent it from choosing another angle, that's why I tried to use the repeat element to try repeat the same angle in away let the vector select angle repeat the same value. I hope I let you get my point.

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

採用された回答

Sulaymon Eshkabilov
Sulaymon Eshkabilov 2023 年 3 月 4 日
Your assignment is not quite clear but to the best understandinbg of mine. Note that repelem(A,1) if A is a scalar, the result is A. This is how your assignment can be solved, e.g.:
angles = [20 45 60 30 90];
randomize_angles=randperm(numel(angles))
randomize_angles = 1×5
1 3 2 4 5
for i=1:numel(randomize_angles)
select_angle = angles(randomize_angles(i))
if select_angle==angles(i)
select_angle = angles(i)
end
end
select_angle = 20
select_angle = 20
select_angle = 60
select_angle = 45
select_angle = 30
select_angle = 30
select_angle = 90
select_angle = 90
  2 件のコメント
omar th
omar th 2023 年 3 月 6 日
thanks for your replay. yes I think my question wasn't clear, but I wanted to to fix the angle when the condition is satisfied but still the loop test another angles
Sulaymon Eshkabilov
Sulaymon Eshkabilov 2023 年 3 月 6 日
Most welcome! Good luck with your exercise.

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

その他の回答 (1 件)

Voss
Voss 2023 年 3 月 4 日
angles = [ 20 45 60 30 90];
randomize_angles=randperm(numel(angles));
selection_made = false;
for i=1:numel(randomize_angles)
if ~selection_made
select_angle = angles(randomize_angles(i))
end
if condition
selection_made = true;
end
end
  2 件のコメント
omar th
omar th 2023 年 3 月 6 日
Thank you for your answer, I tried this way but doesn't work due to when the condition is satisfied the for loop continues in iteration another angle not fix the angle that condition satisfied at.
Voss
Voss 2023 年 3 月 6 日
After the condition is satisfied, yes, the for loop continues to iterate, but, no, another angle is not assigned to select_angle.
If you want to stop the for loop as soon as the condition is satisfied, use break:
angles = [ 20 45 60 30 90];
randomize_angles=randperm(numel(angles));
for i=1:numel(randomize_angles)
select_angle = angles(randomize_angles(i))
if condition
break
end
end
% now select_angle is your angle, use it from here on

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

カテゴリ

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

製品


リリース

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by