picking numbers from matrix
1 次查看(过去 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!