picking numbers from matrix
2 ビュー (過去 30 日間)
古いコメントを表示
I have a matrix and it contains some numbers. I want to select a random number from each column in this matrix and print it in another single row matrix (one of the numbers except 0). but I don't want to print the 0 values to the new matrix.
example :
my matrix :
0 4 8
5 0 0
0 4 6
2 8 0
0 0 7
new matrix :
2 4 6
how can I do that?
Thank you,
Berfin.
0 件のコメント
採用された回答
Stephen23
2022 年 3 月 17 日
M = [0,4,8;5,0,0;0,4,6;2,8,0;0,0,7]
fh1 = @(v)v(randperm(numel(v),1));
fh2 = @(k)fh1(nonzeros(M(:,k)));
V = arrayfun(fh2,1:size(M,2))
その他の回答 (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!