How to save to different column array

2 次查看(过去 30 天)
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:

回答(3 个)

Adam
Adam 2016-9-5
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.

Thorsten
Thorsten 2016-9-5
Y(:, all(isnan(Y))) = [];

Stephen23
Stephen23 2016-9-5
>> 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

类别

Help CenterFile Exchange 中查找有关 Logical 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by