How can I create a vector of 10 random numbers?

33 ビュー (過去 30 日間)
Ben
Ben 2012 年 10 月 22 日
I am looking to create a vector of 20 random numbers and then use 10 othjer random numbers to replace 10 of the data points with NaN

回答 (2 件)

Thomas
Thomas 2012 年 10 月 22 日
編集済み: Thomas 2012 年 10 月 22 日
IS this what you need
A=rand(20,1) % 20 random numbers use rand or randn
b=randi(20,10,1) % randomly choose 10
A(b)=NaN % replace those 10 chosen above with NaN's

Kye Taylor
Kye Taylor 2012 年 10 月 22 日
If you use an approach like
x = rand(20,1);
idx = randi(20,10,1);
x(idx) = NaN,
you may replace less than ten numbers since there may be repeated integers in idx.
If you need to replace exactly 10 numbers every single time, try
x = rand(20,1);
idx = randperm(20);
x(idx(1:10)) = NaN;

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by