How do I replace the elements of a vector with something else based on conditions?

2 ビュー (過去 30 日間)
Brady Retzlaff
Brady Retzlaff 2015 年 10 月 12 日
コメント済み: Stephen23 2015 年 10 月 12 日
I have a vector A, that is 10 random numbers between 0 and 1. If the number is greater than .5 I want to replace it with 1, otherwise, I want to replace it with zero. I can't figure out how to spit out the modified vector. Help?

回答 (3 件)

Jan
Jan 2015 年 10 月 12 日
x = rand(1,10);
result = (x > 0.5);
The round command would be useful also.

KSSV
KSSV 2015 年 10 月 12 日
編集済み: Walter Roberson 2015 年 10 月 12 日
x = rand(1,10) ;
for i = 1:length(x)
if x(i) > 0.5
x(i) = 1. ;
else
x(i) = 0. ;
end
end
Please note, the above task can also be carried out without for loop.
Regards
Sreenivas

Walter Roberson
Walter Roberson 2015 年 10 月 12 日

カテゴリ

Help Center および File ExchangeMigrate GUIDE Apps についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by