how do I apply an if statement to each row of a column vector?

5 ビュー (過去 30 日間)
Ben Newton
Ben Newton 2020 年 10 月 15 日
コメント済み: Sudhakar Shinde 2020 年 10 月 16 日
i have a 1x10 column vector consisting of random numbers.
and i want to apply the following if statement to each number in the column vector.
if R < 0.5;
step = -1;
elseif R > 0.5;
step = 1;
end
where R is the column vector, and so the solution would be a 1x10 column vector consisting of either +1 or -1
any help is appreciatied, thanks.

採用された回答

Sudhakar Shinde
Sudhakar Shinde 2020 年 10 月 15 日
編集済み: Sudhakar Shinde 2020 年 10 月 15 日
Try this:
R = [0.1:0.1:1];
step=zeros(1,length(R(:)));
for n=1:length(R(:))
if R(n)<0.5
step(n)= -1;
elseif R(n) > 0.5
step(n) = 1;
end
end
  2 件のコメント
Ben Newton
Ben Newton 2020 年 10 月 15 日
thanks for your help
Sudhakar Shinde
Sudhakar Shinde 2020 年 10 月 16 日
Welcome. Glad to help.

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeNaNs についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by