Determine less than in for loop

14 ビュー (過去 30 日間)
Alexander Madsen
Alexander Madsen 2021 年 9 月 10 日
コメント済み: Mathieu NOE 2021 年 9 月 10 日
I have a problem with my for loop. I would like to determine if a value in the loop is less than the previous value and then change the value if so.
for ii = (1:length(sz));
z(ii);
if z(ii+1) < z(ii)
z = 180-z;
end
z
end
I get the error "Index exceeds the number of array elements (7)." at the if statement.
i know what it means but can not see a way to avoid it.

採用された回答

Mathieu NOE
Mathieu NOE 2021 年 9 月 10 日
hello
does that reflect what you wanted to do ?
clc
clearvars
% x = round(100*rand(1,10));
x = [11 96 0 77 82 87 8 40 26 80];
z = x ;
for ii = (1:length(z)-1) % change the last iteration value : length(z)-1 and not length(z)
% z(ii);
if z(ii+1) < z(ii)
% z = 180-z; % this will modify the entire z vector
z(ii+1) = 180-z(ii+1); % this will modify the current z value only (my guess)
end
% z
end
  2 件のコメント
Alexander Madsen
Alexander Madsen 2021 年 9 月 10 日
Thanks alot, it works
Mathieu NOE
Mathieu NOE 2021 年 9 月 10 日
My pleasure

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

その他の回答 (0 件)

カテゴリ

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

製品


リリース

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by