picking numbers from matrix
7 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Berfin Çetinkaya
le 17 Mar 2022
Commenté : Berfin Çetinkaya
le 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 commentaires
Réponse acceptée
Stephen23
le 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))
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Creating and Concatenating Matrices dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!