picking numbers from matrix
2 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
Berfin Çetinkaya
il 17 Mar 2022
Commentato: Berfin Çetinkaya
il 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 Commenti
Risposta accettata
Stephen23
il 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))
Più risposte (0)
Vedere anche
Categorie
Scopri di più su Creating and Concatenating Matrices in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!