picking numbers from matrix
1 view (last 30 days)
Show older comments
Berfin Çetinkaya
on 17 Mar 2022
Commented: Berfin Çetinkaya
on 17 Mar 2022
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 Comments
Accepted Answer
Stephen23
on 17 Mar 2022
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))
More Answers (0)
See Also
Categories
Find more on Operating on Diagonal Matrices in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!