How i will choose a random element of a matrix
46 ビュー (過去 30 日間)
古いコメントを表示
I have a matrix A,and i want to choose randomly an element from the matrix.How i will do that?
0 件のコメント
採用された回答
per isakson
2015 年 3 月 24 日
One way
A = rand(3,4)
A( randi([1,size(A,1)],1), randi([1,size(A,2)],1) )
2 件のコメント
Assen Beshr
2022 年 5 月 21 日
編集済み: Assen Beshr
2022 年 5 月 21 日
per isakson: perfect . I have one question related to this question. the question is How I choose random value npop X n matrix if npop =5 and n=2; from column matrix L=[2;4;5;7;8;9;12;25;32;33]
for example [4 25
2 7
33 12
32 5
4 9];
その他の回答 (1 件)
Guillaume
2015 年 3 月 24 日
It may be (insignificantly) faster to use linear indexing:
A(randi(numel(A)));
It's more succinct anyway, and works whatever the number of dimensions of the array.
0 件のコメント
参考
カテゴリ
Help Center および File Exchange で Operating on Diagonal Matrices についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!