Got Questions? Get Answers.
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:
need help to re-arrange matrix

Subject: need help to re-arrange matrix

From: Arie Driga

Date: 21 Sep, 2010 16:26:21

Message: 1 of 8

Dear all,

I have a matrix:
a=[5 6 7; 8 9 10; 11 12 13]
which a a 3x3 matrix
The cell location corresponds to the coordinate and the number correspond to the value.
I would like to be able to plot the data in the tecplot, so I have to re-arrange the matrix into:

1 1 5
1 2 6
1 3 7
. . .
. . .
. . .
3 3 13

the first column is x axis, the second column is for y-axis and the last column is for the value..

Hope someone can help me on this..

Thanks in advance

Subject: need help to re-arrange matrix

From: Sean

Date: 21 Sep, 2010 16:36:38

Message: 2 of 8

"Arie Driga" <a_driga@yahoo.com> wrote in message <i7ambd$4g5$1@fred.mathworks.com>...
> Dear all,
>
> I have a matrix:
> a=[5 6 7; 8 9 10; 11 12 13]
> which a a 3x3 matrix
> The cell location corresponds to the coordinate and the number correspond to the value.
> I would like to be able to plot the data in the tecplot, so I have to re-arrange the matrix into:
>
> 1 1 5
> 1 2 6
> 1 3 7
> . . .
> . . .
> . . .
> 3 3 13
>
> the first column is x axis, the second column is for y-axis and the last column is for the value..
>
> Hope someone can help me on this..
>
> Thanks in advance

[xx yy] = meshgrid(1:3);
new_matrix = [xx(:),yy(:),a(:)];

Subject: need help to re-arrange matrix

From: Arie Driga

Date: 21 Sep, 2010 16:50:26

Message: 3 of 8

"Sean " <sean.dewolski@nospamplease.umit.maine.edu> wrote in message <i7amum$c24$1@fred.mathworks.com>...
> "Arie Driga" <a_driga@yahoo.com> wrote in message <i7ambd$4g5$1@fred.mathworks.com>...

>
> [xx yy] = meshgrid(1:3);
> new_matrix = [xx(:),yy(:),a(:)];

Thanks Sean!
It works, but it seems that it only works for a square matrix
what if I have a rectangular matrix
I tried with 3x4 matrix
when I put your command "[xx(:),yy(:),a(:)];"
it gives error message:
??? Error using ==> horzcat
CAT arguments dimensions are not consistent.

Subject: need help to re-arrange matrix

From: Steven_Lord

Date: 21 Sep, 2010 17:26:34

Message: 4 of 8



"Arie Driga" <a_driga@yahoo.com> wrote in message
news:i7anoi$4at$1@fred.mathworks.com...
> "Sean " <sean.dewolski@nospamplease.umit.maine.edu> wrote in message
> <i7amum$c24$1@fred.mathworks.com>...
>> "Arie Driga" <a_driga@yahoo.com> wrote in message
>> <i7ambd$4g5$1@fred.mathworks.com>...
>
>>
>> [xx yy] = meshgrid(1:3);
>> new_matrix = [xx(:),yy(:),a(:)];
>
> Thanks Sean!
> It works, but it seems that it only works for a square matrix
> what if I have a rectangular matrix
> I tried with 3x4 matrix
> when I put your command "[xx(:),yy(:),a(:)];"
> it gives error message:
> ??? Error using ==> horzcat
> CAT arguments dimensions are not consistent.

Then you need to modify the MESHGRID call by adding another input argument.
I'll let you experiment and determine what that input should be and whether
it should come before or after the 1:3 that's already there.

--
Steve Lord
slord@mathworks.com
comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ
To contact Technical Support use the Contact Us link on
http://www.mathworks.com

Subject: need help to re-arrange matrix

From: Sean

Date: 21 Sep, 2010 17:29:27

Message: 5 of 8

"Arie Driga" <a_driga@yahoo.com> wrote in message <i7anoi$4at$1@fred.mathworks.com>...
> "Sean " <sean.dewolski@nospamplease.umit.maine.edu> wrote in message <i7amum$c24$1@fred.mathworks.com>...
> > "Arie Driga" <a_driga@yahoo.com> wrote in message <i7ambd$4g5$1@fred.mathworks.com>...
>
> >
> > [xx yy] = meshgrid(1:3);
> > new_matrix = [xx(:),yy(:),a(:)];
>
> Thanks Sean!
> It works, but it seems that it only works for a square matrix
> what if I have a rectangular matrix
> I tried with 3x4 matrix
> when I put your command "[xx(:),yy(:),a(:)];"
> it gives error message:
> ??? Error using ==> horzcat
> CAT arguments dimensions are not consistent.

Then you would need:
[xx yy] = meshgrid(1:3,1:4); %note dimensions
new_matrix = [xx(:),yy(:),a(:)];

A general way would be:
[xx yy] = meshgrid(1:size(a,1),1:size(a,2));
new_matrix = [xx(:),yy(:),a(:)];

Subject: need help to re-arrange matrix

From: Sean

Date: 21 Sep, 2010 17:38:06

Message: 6 of 8

> [xx yy] = meshgrid(1:3,1:4); %note dimensions
> [xx yy] = meshgrid(1:size(a,1),1:size(a,2));


I got those backwards for meshgrid. They would be the correct inputs to ndgrid()
> [xx yy] = meshgrid(1:size(a,2),1:size(a,1));

Subject: need help to re-arrange matrix

From: Oleg Komarov

Date: 21 Sep, 2010 17:54:19

Message: 7 of 8

"Arie Driga" <a_driga@yahoo.com> wrote in message <i7ambd$4g5$1@fred.mathworks.com>...
> Dear all,
>
> I have a matrix:
> a=[5 6 7; 8 9 10; 11 12 13]
> which a a 3x3 matrix
> The cell location corresponds to the coordinate and the number correspond to the value.
> I would like to be able to plot the data in the tecplot, so I have to re-arrange the matrix into:
>
> 1 1 5
> 1 2 6
> 1 3 7
> . . .
> . . .
> . . .
> 3 3 13
>
> the first column is x axis, the second column is for y-axis and the last column is for the value..
>
> Hope someone can help me on this..
>
> Thanks in advance

Another solution:

% Add headers
a = [NaN 1:size(a,2); [(1:size(a,1)).' a]]
a =
   NaN 1 2 3
     1 5 6 7
     2 8 9 10
     3 11 12 13

>> % unpivot (1st col - colheaders; 2nd col - rowheaders; 3rd col - values)
a = unPivot(a) % on FEX: http://www.mathworks.com/matlabcentral/fileexchange/26119
a =
     1 1 5
     1 2 8
     1 3 11
     2 1 6
     2 2 9
     2 3 12
     3 1 7
     3 2 10
     3 3 13

You can play with unPivot sorting the output by row headers:
unPivot(a,2)
ans =
     1 1 5
     2 1 6
     3 1 7
     1 2 8
     2 2 9
     3 2 10
     1 3 11
     2 3 12
     3 3 13

Oleg

Subject: need help to re-arrange matrix

From: Matt Fig

Date: 21 Sep, 2010 17:56:04

Message: 8 of 8

"Sean " <sean.dewolski@nospamplease.umit.maine.edu> wrote in message <i7aqhu$90f$1@fred.mathworks.com>...
> > [xx yy] = meshgrid(1:3,1:4); %note dimensions
> > [xx yy] = meshgrid(1:size(a,1),1:size(a,2));
>
>
> I got those backwards for meshgrid. They would be the correct inputs to ndgrid()
> > [xx yy] = meshgrid(1:size(a,2),1:size(a,1));

Also, a slightly more memory efficient version (in case MESHGRID throws you out!):

I = (1:numel(a)).';
I = [ceil(I/size(a,1)),rem(I-1,size(a,1))+1,a(:)]

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