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:
Reshaping

Subject: Reshaping

From: Kurt

Date: 15 Oct, 2010 00:28:05

Message: 1 of 9

hello friends,

i have a matrix I am not sure how to manipulate in order to reshape it.
The matrix is:

H =
             21 11 14 31
              7 84 29 55
             40 93 36 1

I wish to get it reshaped like;

H =
             21 11 7 84 40 93
             14 31 29 55 36 1

help would be much appreciated.

Kurtis

Subject: Reshaping

From: Nathan

Date: 15 Oct, 2010 00:42:12

Message: 2 of 9

On Oct 14, 5:28 pm, "Kurt " <rerty...@gmail.com> wrote:
> hello friends,
>
> i have a matrix I am not sure how to manipulate in order to reshape it.
> The matrix is:
>
> H =
>              21    11     14     31
>               7     84     29     55
>              40    93      36      1
>
> I wish to get it reshaped like;
>
> H =
>              21    11     7     84    40    93
>              14    31     29   55    36    1
>
> help would be much appreciated.
>
> Kurtis

One way:
(This should all be on one line: )

H = cell2mat(cellfun(@(x)reshape(x,length(x)/2,
[])',mat2cell(H,repmat(1,size(H,1),1),size(H,2)),'un',0)');
%%%%%%%%%%%%%%
H =
    21 11 7 84 40 93
    14 31 29 55 36 1

-Nathan

Subject: Reshaping

From: Kurt

Date: 15 Oct, 2010 01:07:03

Message: 3 of 9

wow,

thanks nathan. Can i ask how do you reach to this level of coming up with such code?
I'd like to be there one day.

thank
all the best
Kurt

Subject: Reshaping

From: James Tursa

Date: 15 Oct, 2010 01:21:04

Message: 4 of 9

"Kurt " <rerty258@gmail.com> wrote in message <i9876l$hs5$1@fred.mathworks.com>...
> hello friends,
>
> i have a matrix I am not sure how to manipulate in order to reshape it.
> The matrix is:
>
> H =
> 21 11 14 31
> 7 84 29 55
> 40 93 36 1
>
> I wish to get it reshaped like;
>
> H =
> 21 11 7 84 40 93
> 14 31 29 55 36 1
>
> help would be much appreciated.
>
> Kurtis

Another method:

reshape(mtimesx(1,reshape(H.',2,2,[]),'T'),2,[])

You can find the mtimesx function on the FEX:

http://www.mathworks.com/matlabcentral/fileexchange/25977-mtimesx-fast-matrix-multiply-with-multi-dimensional-support

James Tursa

Subject: Reshaping

From: Roger Stafford

Date: 15 Oct, 2010 06:20:05

Message: 5 of 9

"Kurt " <rerty258@gmail.com> wrote in message <i9876l$hs5$1@fred.mathworks.com>...
> hello friends,
>
> i have a matrix I am not sure how to manipulate in order to reshape it.
> The matrix is:
>
> H =
> 21 11 14 31
> 7 84 29 55
> 40 93 36 1
>
> I wish to get it reshaped like;
>
> H =
> 21 11 7 84 40 93
> 14 31 29 55 36 1
>
> help would be much appreciated.
>
> Kurtis
- - - - - - - - - - - -
  Here is yet another method. It assumes that the original H has an even number of columns (2*m). (I hope the following is the generalization on the size of H that you had in mind.)

[n,m2] = size(H); m = m2/2; % H is an n by 2*m array
[q,p] = meshgrid(1:2*n,1:m);
r = ceil(q/2)+n*(2*p-mod(q,2)-1);
H2 = H(r);

The generated H2 should be an m by 2*n array. In your example n = 3 and m = 2.

  Once r is obtained it can be used on any number of arrays of the size of H.

Roger Stafford

Subject: Reshaping

From: Jan Simon

Date: 15 Oct, 2010 10:47:03

Message: 6 of 9

Dear Kurt,

> H =
> 21 11 14 31
> 7 84 29 55
> 40 93 36 1
> H2 =
> 21 11 7 84 40 93
> 14 31 29 55 36 1

H2 = reshape(permute(reshape(H, 3,2,2), [3,2,1]), 2, 6)

Jan

Subject: Reshaping

From: Steven_Lord

Date: 15 Oct, 2010 14:23:43

Message: 7 of 9



"Kurt " <rerty258@gmail.com> wrote in message
news:i989fn$eds$1@fred.mathworks.com...
> wow,
>
> thanks nathan. Can i ask how do you reach to this level of coming up with
> such code?
> I'd like to be there one day.

Practice, practice, and even more practice.

That and trying to read and understand the code that's posted in CSSM helps
too.

--
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: Reshaping

From: James Tursa

Date: 15 Oct, 2010 15:49:04

Message: 8 of 9

"Steven_Lord" <slord@mathworks.com> wrote in message <i99o5g$kes$1@fred.mathworks.com>...
>
> "Kurt " <rerty258@gmail.com> wrote in message
> news:i989fn$eds$1@fred.mathworks.com...
> > wow,
> >
> > thanks nathan. Can i ask how do you reach to this level of coming up with
> > such code?
> > I'd like to be there one day.
>
> Practice, practice, and even more practice.

How do I get to Carnegie Hall?

Subject: Reshaping

From: Steven_Lord

Date: 15 Oct, 2010 17:14:35

Message: 9 of 9



"James Tursa" <aclassyguy_with_a_k_not_a_c@hotmail.com> wrote in message
news:i99t5g$p71$1@fred.mathworks.com...
> "Steven_Lord" <slord@mathworks.com> wrote in message
> <i99o5g$kes$1@fred.mathworks.com>...
>>
>> "Kurt " <rerty258@gmail.com> wrote in message
>> news:i989fn$eds$1@fred.mathworks.com...
>> > wow,
>> >
>> > thanks nathan. Can i ask how do you reach to this level of coming up
>> > with such code?
>> > I'd like to be there one day.
>>
>> Practice, practice, and even more practice.
>
> How do I get to Carnegie Hall?

I don't know about you, but if I were going there right now I'd consider
this route:

http://maps.google.com/maps?f=d&source=s_d&saddr=3+Apple+Hill+Dr,+Natick,+MA+01760&daddr=Carnegie+Hall,+New+York,+NY

:)

--
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

Tags for 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