How to save to different column array
6 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Hi, I have below matrix and I want to save each column into different arrays (or single column matrix) if at least one row exists a numeric value(because 3rd column does not contain any numeric values, all "NA"):
Input matrix:
1 3 NA 5
2 NA NA NA
5 NA NA NA
6 NA NA 8
My output should be:
Y1:
1
2
5
6
Y2:
3
NA
NA
NA
Y3:
5
NA
NA
8
Please someone help me how to do this,many thanks in advance:
0 commentaires
Réponses (3)
Adam
le 5 Sep 2016
Y = [1 3 NaN 5; 2 NaN NaN NaN; 5 NaN NaN NaN; 6 NaN NaN 8]
Y( :, sum( isnan( Y ) ) == size( Y, 1 ) ) = [];
will remove the columns you don't want, in-place. You can obviously take a copy of your matrix if you still want the original.
There is no reason why you should split it into individually named variables though.
Y(:,1), Y(:,2) works far better than Y1, Y2 in further code.
0 commentaires
Stephen23
le 5 Sep 2016
>> M = [...
1, 3,NaN, 5
2,NaN,NaN,NaN
5,NaN,NaN,NaN
6,NaN,NaN, 8];
>> out = M(:,any(isfinite(M),1))
out =
1 3 5
2 NaN NaN
5 NaN NaN
6 NaN 8
>> out(:,1)
ans =
1
2
5
6
0 commentaires
Voir également
Catégories
En savoir plus sur Logical 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!