How to save to different column array

조회 수: 5 (최근 30일)
Mekala balaji
Mekala balaji 2016년 9월 5일
답변: Stephen23 2016년 9월 5일
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