lisandrojim
lisandrojim

Reputation: 509

How to take the rows of a matrix and save them in a cell array with MATLAB

this is my question:

I have this matrix A:

A=[1 2 3; 4 5 NaN; 6 8 9];

And I want to make something like this:

[B] = somefuntion(A)

Where B will have the next information:

B={[1 2 3];[4 5 NaN];[6 8 9]};

I hope that you can help me, thanks by the way!.

Upvotes: 1

Views: 60

Answers (2)

lisandrojim
lisandrojim

Reputation: 509

A=[1 2 3; 4 5 NaN; 6 8 9];

C = mat2cell(A,ones(1,size(A,1)),size(A,2))

Results:

C{1}=[ 1     2     3];
C{2}=[ 4     5   NaN];
C{3}=[ 6     8     9];

Upvotes: 2

KatyB
KatyB

Reputation: 3990

Couldn't you just use a simple loop?

for i = 1:size(A,2);
    B{i} = A(i,:);
end
B = B';

Upvotes: 0

Related Questions