How to save to different column array

6 visualizaciones (últimos 30 días)
Mekala balaji
Mekala balaji el 5 de Sept. de 2016
Respondida: Stephen23 el 5 de Sept. de 2016
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:

Respuestas (3)

Adam
Adam el 5 de Sept. de 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.

Thorsten
Thorsten el 5 de Sept. de 2016
Y(:, all(isnan(Y))) = [];

Stephen23
Stephen23 el 5 de Sept. de 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

Categorías

Más información sobre Logical en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by