randomly select a number from a matrix

12 ビュー (過去 30 日間)
Camilla Santicoli
Camilla Santicoli 2015 年 5 月 19 日
編集済み: Andrei Bobrov 2015 年 5 月 19 日
Hi all. I have a 365x27 matrix of values where the rows are the different days of a year (365) and the columns the number of years that I am considering (27). What I would like to do is to create a vector 365x1 where every day one value is randomly selected from the initial matrix. So, for the first day the function should randomly select a value out of the first row of the inital matrix, for the second day a value randomly taken from the second row of the inital matrix and so on. Does someone know how to do it? Thanks

採用された回答

Torsten
Torsten 2015 年 5 月 19 日
indexarray=randi(27,365,1);
For k=1:365
Vector(k)=Matrix(k,indexarray(k,1));
end
Best wishes
Torsten.
  1 件のコメント
Camilla Santicoli
Camilla Santicoli 2015 年 5 月 19 日
Thank you very much

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

その他の回答 (1 件)

Andrei Bobrov
Andrei Bobrov 2015 年 5 月 19 日
編集済み: Andrei Bobrov 2015 年 5 月 19 日
n = size(Matrix);
out = Matrix(sub2ind(n,(1:n(1))',randi(n(2),n(1),1)));
  1 件のコメント
Camilla Santicoli
Camilla Santicoli 2015 年 5 月 19 日
thank you Andrei

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

カテゴリ

Help Center および File ExchangeCreating and Concatenating Matrices についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by