How to save to different column array
6 views (last 30 days)
Show older comments
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 Comments
Answers (3)
Adam
on 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 Comments
Stephen23
on 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 Comments
See Also
Categories
Find more on Logical 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!