Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
rearranging a matrix

Subject: rearranging a matrix

From: Florian

Date: 18 Feb, 2012 14:52:11

Message: 1 of 3

Hi all,

I loaded data from excel into matlab and it is now in the following format:

NaN NaN NaN
NaN NaN NaN
1 2 3
4 5 6
NaN NaN NaN
NaN NaN NaN
7 8 9
10 11 12
NaN NaN NaN
NaN NaN NaN
13 14 15
16 17 18

How can I arrange these data into the following format?

1 2 3 4 5 6
7 8 9 10 11 12
13 14 15 16 17 18

Best regards!

Subject: rearranging a matrix

From: Sadik

Date: 18 Feb, 2012 23:58:31

Message: 2 of 3

Hi Florian,

Given

A = [NaN NaN NaN
NaN NaN NaN
1 2 3
4 5 6
NaN NaN NaN
7 8 9
10 11 12
NaN NaN NaN
13 14 15
16 17 18];

one solution:

A = A';
B = A(~isnan(A));
C = reshape(B,[6 length(B)/6])';

Best.

Subject: rearranging a matrix

From: james bejon

Date: 19 Feb, 2012 00:15:22

Message: 3 of 3

Another way. No better really. (Unless you're a fan of unclear one-liners)

a = [NaN NaN NaN
NaN NaN NaN
1 2 3
4 5 6
NaN NaN NaN
7 8 9
10 11 12
NaN NaN NaN
13 14 15
16 17 18];

reshape( a(~any(isnan(a), 2), :).', 6, [] ).'

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us