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:
permute/reshape

Subject: permute/reshape

From: Jane

Date: 17 May, 2011 01:13:03

Message: 1 of 21

Hi,

Can someone please help me Permute/reshape the following. I am finding it difficult.

Input
A1A2A3
A1A2A3
A1A2A3
B1B2B3
B1B2B3
B1B2B3
C1C2C3
C1C2C3
C1C2C3

Output
A1B1C1 A1B1C1 A1B1C1 A1B1C1
A2B2C2 A2B2C2 A2B2C2 A2B2C2
A3B3C3 A3B3C3 A3B3C3 A3B3C3

 Any help appreciated,
Thanks

Subject: permute/reshape

From: ImageAnalyst

Date: 17 May, 2011 01:20:39

Message: 2 of 21

Can you give us the actual MATLAB code to make up your sample "Input"
matrix? I don't want to generate the code for you - I think you
should do that since it would be for your benefit. That would make it
easier for us to experiment with your code.

By the way, don't call it input, at least not with all lower case, or
you'll be overriding a built-in MATLAB function.

Subject: permute/reshape

From: Jane

Date: 17 May, 2011 01:32:02

Message: 3 of 21


T=[BB(1:76,K1(1:3:363));BB(1:76,K1(2:3:363));BB(1:76,K1(3:3:363))]';

I want it back into the format [1:76 x 1:363].

Thanks

Subject: permute/reshape

From: Nasser M. Abbasi

Date: 17 May, 2011 01:44:42

Message: 4 of 21

On 5/16/2011 6:32 PM, Jane wrote:
>
> T=[BB(1:76,K1(1:3:363));BB(1:76,K1(2:3:363));BB(1:76,K1(3:3:363))]';
>
> I want it back into the format [1:76 x 1:363].
>
> Thanks


T=[BB(1:76,K1(1:3:363));BB(1:76,K1(2:3:363));BB(1:76,K1(3:3:363))]'
??? Undefined function or method 'K1' for input arguments of type 'double'.

It is really ridiculous that you won't take the time to write
an actual small sample code to show the data you want others
to use, but you want someone to spend their time to help you.

What did you expect someone to do with the above?

just amazing.

--Nasser

Subject: permute/reshape

From: Jane

Date: 17 May, 2011 01:54:03

Message: 5 of 21

I am not able to generate code to give to you sorry. I think I need to figure out which dimension to send part of the data. The above just shows how the data was reshaped to a new format which I want to get back to.

Subject: permute/reshape

From: Nasser M. Abbasi

Date: 17 May, 2011 01:57:18

Message: 6 of 21

On 5/16/2011 6:54 PM, Jane wrote:
> I am not able to generate code to give to you sorry. I think I need to figure
>out which dimension to send part of the data. The above just shows how the
>data was reshaped to a new format which I want to get back to.

You were asked just to provide data. not code.

The data you gave first can't be used to test with.

can't you type type some example matrix you called INPUT?

How do you expect someone to help you if you do not want
to even do that?

Like A=[1 1; 2 3; etc....

Just make it up !

--Nasser

Subject: permute/reshape

From: ImageAnalyst

Date: 17 May, 2011 02:23:03

Message: 7 of 21

On May 16, 9:54 pm, "Jane " <J...@mytrashmail.com> wrote:
> I am not able to generate code to give to you sorry.
-----------------------------
If it's too much trouble for you, then it's certainly too much trouble
for me.

Subject: permute/reshape

From: Jane

Date: 17 May, 2011 02:27:03

Message: 8 of 21

ImageAnalyst <imageanalyst@mailinator.com> wrote in message <cbcffc55-943b-4094-8b23-fc7fe1777212@s11g2000yqj.googlegroups.com>...
> On May 16, 9:54 pm, "Jane " <J...@mytrashmail.com> wrote:
> > I am not able to generate code to give to you sorry.
> -----------------------------
> If it's too much trouble for you, then it's certainly too much trouble
> for me.

Wait I am still trying to generate something for you

Subject: permute/reshape

From: Jane

Date: 17 May, 2011 02:46:04

Message: 9 of 21

A=[1 2 3 4 5 6 7 8 9 ; 10 11 12 13 14 15 16 17 18 ; 19 20 21 22 23 24 25 26 27];
B=[1 4 7 ; 10 13 16 ; 19 22 25 ; 2 5 8 ; 11 14 17 ; 20 23 26; 3 6 9; 12 15 18; 21 24 27]';

Put B numbers back in the same order as A. Hope this makes sense.

Subject: permute/reshape

From: Roger Stafford

Date: 17 May, 2011 03:05:04

Message: 10 of 21

"Jane" wrote in message <iqsnhc$9bt$1@newscl01ah.mathworks.com>...
> A=[1 2 3 4 5 6 7 8 9 ; 10 11 12 13 14 15 16 17 18 ; 19 20 21 22 23 24 25 26 27];
> B=[1 4 7 ; 10 13 16 ; 19 22 25 ; 2 5 8 ; 11 14 17 ; 20 23 26; 3 6 9; 12 15 18; 21 24 27]';
>
> Put B numbers back in the same order as A. Hope this makes sense.
- - - - - - - - - -
 A2 = reshape(reshape(B,9,3).',9,3).';

Roger Stafford

Subject: permute/reshape

From: Jane

Date: 17 May, 2011 03:14:04

Message: 11 of 21

"Roger Stafford" wrote in message <iqsol0$cff$1@newscl01ah.mathworks.com>...
> "Jane" wrote in message <iqsnhc$9bt$1@newscl01ah.mathworks.com>...
> > A=[1 2 3 4 5 6 7 8 9 ; 10 11 12 13 14 15 16 17 18 ; 19 20 21 22 23 24 25 26 27];
> > B=[1 4 7 ; 10 13 16 ; 19 22 25 ; 2 5 8 ; 11 14 17 ; 20 23 26; 3 6 9; 12 15 18; 21 24 27]';
> >
> > Put B numbers back in the same order as A. Hope this makes sense.
> - - - - - - - - - -
> A2 = reshape(reshape(B,9,3).',9,3).';
>
> Roger Stafford

Thanks. I will try to apply this to my data and report back.

Subject: permute/reshape

From: Jane

Date: 17 May, 2011 04:59:03

Message: 12 of 21

"Jane" wrote in message <iqsp5s$dvg$1@newscl01ah.mathworks.com>...
> "Roger Stafford" wrote in message <iqsol0$cff$1@newscl01ah.mathworks.com>...
> > "Jane" wrote in message <iqsnhc$9bt$1@newscl01ah.mathworks.com>...
> > > A=[1 2 3 4 5 6 7 8 9 ; 10 11 12 13 14 15 16 17 18 ; 19 20 21 22 23 24 25 26 27];
> > > B=[1 4 7 ; 10 13 16 ; 19 22 25 ; 2 5 8 ; 11 14 17 ; 20 23 26; 3 6 9; 12 15 18; 21 24 27]';
> > >
> > > Put B numbers back in the same order as A. Hope this makes sense.
> > - - - - - - - - - -
> > A2 = reshape(reshape(B,9,3).',9,3).';
> >
> > Roger Stafford
>
> Thanks. I will try to apply this to my data and report back.

Hi, I couldn't get it to work for my data. Not sure if I gave the correct example. How do I get B back to A for the following? Just to test again.

A=[1 2 3;
   7 8 9;
   13 14 15;
   19 20 21;
   25 26 27;
   31 32 33;
   37 38 39;
   43 44 45;
   49 50 51];

B=[1 19 37 2 20 38 3 21 39; 7 25 43 8 26 44 9 27 45; 13 31 49 14 32 50 15 33 51];

Thanks

Subject: permute/reshape

From: Jane

Date: 17 May, 2011 05:24:03

Message: 13 of 21

Please help me Convert this B to A matrix. I believe this is what I need.

A=[1 2 3 4 5 6;
   7 8 9 10 11 12;
   13 14 15 16 17 18;
   19 20 21 22 23 24;
   25 26 27 28 29 30;
   31 32 33 34 35 36;
   37 38 39 40 41 42;
   43 44 45 46 47 48;
   49 50 51 52 53 54];

B=[1 4;
   7 10;
   13 16;
   19 22;
   25 28;
   31 34;
   37 40;
   43 46;
   49 52;
   2 5;
   8 11;
   14 17;
   20 23;
   26 29;
   32 35;
   38 41;
   44 47;
   50 53;
   3 6;
   9 12;
   15 18;
   21 24;
   27 30;
   33 36;
   39 42;
   45 48;
   51 54]';

Subject: permute/reshape

From: Nasser M. Abbasi

Date: 17 May, 2011 05:42:02

Message: 14 of 21

On 5/16/2011 10:24 PM, Jane wrote:
> Please help me Convert this B to A matrix. I believe this is what I need.
>
> A=[1 2 3 4 5 6;
> 7 8 9 10 11 12;
> 13 14 15 16 17 18;
> 19 20 21 22 23 24;
> 25 26 27 28 29 30;
> 31 32 33 34 35 36;
> 37 38 39 40 41 42;
> 43 44 45 46 47 48;
> 49 50 51 52 53 54];
>
> B=[1 4;
> 7 10;
> 13 16;
> 19 22;
> 25 28;
> 31 34;
> 37 40;
> 43 46;
> 49 52;
> 2 5;
> 8 11;
> 14 17;
> 20 23;
> 26 29;
> 32 35;
> 38 41;
> 44 47;
> 50 53;
> 3 6;
> 9 12;
> 15 18;
> 21 24;
> 27 30;
> 33 36;
> 39 42;
> 45 48;
> 51 54]';



May be

-------------------------
N=size(A,2);
r=arrayfun(@(i) cat(2,A(:,i),A(:,i+N/2))',1:N/2,'UniformOutput',false);
B=transpose([r{:}])
----------------------------

B =

      1 4
      7 10
     13 16
     19 22
     25 28
     31 34
     37 40
     43 46
     49 52
      2 5
      8 11
     14 17
     20 23
     26 29
     32 35
     38 41
     44 47
     50 53
      3 6
      9 12
     15 18
     21 24
     27 30
     33 36
     39 42
     45 48
     51 54

--Nasser

Subject: permute/reshape

From: Jane

Date: 17 May, 2011 05:57:03

Message: 15 of 21

"Nasser M. Abbasi" <nma@12000.org> wrote in message <iqt1ra$4nc$1@speranza.aioe.org>...
> On 5/16/2011 10:24 PM, Jane wrote:
> > Please help me Convert this B to A matrix. I believe this is what I need.
> >
> > A=[1 2 3 4 5 6;
> > 7 8 9 10 11 12;
> > 13 14 15 16 17 18;
> > 19 20 21 22 23 24;
> > 25 26 27 28 29 30;
> > 31 32 33 34 35 36;
> > 37 38 39 40 41 42;
> > 43 44 45 46 47 48;
> > 49 50 51 52 53 54];
> >
> > B=[1 4;
> > 7 10;
> > 13 16;
> > 19 22;
> > 25 28;
> > 31 34;
> > 37 40;
> > 43 46;
> > 49 52;
> > 2 5;
> > 8 11;
> > 14 17;
> > 20 23;
> > 26 29;
> > 32 35;
> > 38 41;
> > 44 47;
> > 50 53;
> > 3 6;
> > 9 12;
> > 15 18;
> > 21 24;
> > 27 30;
> > 33 36;
> > 39 42;
> > 45 48;
> > 51 54]';
>
>
>
> May be
>
> -------------------------
> N=size(A,2);
> r=arrayfun(@(i) cat(2,A(:,i),A(:,i+N/2))',1:N/2,'UniformOutput',false);
> B=transpose([r{:}])
> ----------------------------
>
> B =
>
> 1 4
> 7 10
> 13 16
> 19 22
> 25 28
> 31 34
> 37 40
> 43 46
> 49 52
> 2 5
> 8 11
> 14 17
> 20 23
> 26 29
> 32 35
> 38 41
> 44 47
> 50 53
> 3 6
> 9 12
> 15 18
> 21 24
> 27 30
> 33 36
> 39 42
> 45 48
> 51 54
>
> --Nasser

Thank you Nasser. That code gives B matrix from A. How can I do that in reverse. Output A. B is known.

Subject: permute/reshape

From: Nasser M. Abbasi

Date: 17 May, 2011 06:14:31

Message: 16 of 21

On 5/16/2011 10:57 PM, Jane wrote:
> "Nasser M. Abbasi"<nma@12000.org> wrote in message<iqt1ra$4nc$1@speranza.aioe.org>...
>> On 5/16/2011 10:24 PM, Jane wrote:
>>> Please help me Convert this B to A matrix. I believe this is what I need.
>>>
>>> A=[1 2 3 4 5 6;
>>> 7 8 9 10 11 12;
>>> 13 14 15 16 17 18;
>>> 19 20 21 22 23 24;
>>> 25 26 27 28 29 30;
>>> 31 32 33 34 35 36;
>>> 37 38 39 40 41 42;
>>> 43 44 45 46 47 48;
>>> 49 50 51 52 53 54];
>>>
>>> B=[1 4;
>>> 7 10;
>>> 13 16;
>>> 19 22;
>>> 25 28;
>>> 31 34;
>>> 37 40;
>>> 43 46;
>>> 49 52;
>>> 2 5;
>>> 8 11;
>>> 14 17;
>>> 20 23;
>>> 26 29;
>>> 32 35;
>>> 38 41;
>>> 44 47;
>>> 50 53;
>>> 3 6;
>>> 9 12;
>>> 15 18;
>>> 21 24;
>>> 27 30;
>>> 33 36;
>>> 39 42;
>>> 45 48;
>>> 51 54]';
>>
>>
>>
>> May be
>>
>> -------------------------
>> N=size(A,2);
>> r=arrayfun(@(i) cat(2,A(:,i),A(:,i+N/2))',1:N/2,'UniformOutput',false);
>> B=transpose([r{:}])
>> ----------------------------
>>
>> B =
>>
>> 1 4
>> 7 10
>> 13 16
>> 19 22
>> 25 28
>> 31 34
>> 37 40
>> 43 46
>> 49 52
>> 2 5
>> 8 11
>> 14 17
>> 20 23
>> 26 29
>> 32 35
>> 38 41
>> 44 47
>> 50 53
>> 3 6
>> 9 12
>> 15 18
>> 21 24
>> 27 30
>> 33 36
>> 39 42
>> 45 48
>> 51 54
>>
>> --Nasser
>
> Thank you Nasser. That code gives B matrix from A. How can I do that in
>reverse. Output A. B is known.

Btw, here is another solution just thought off. You can check which is more efficnent:

---------------------------------------------
nRow = size(A,2); nCol=size(A,1);
B = reshape( [A(:,1:nRow/2) A(:,nRow/2+1:nRow) ], nRow*nCol/2 ,2 )
-------------------------------------------------

This will give same output.

Will get back to you later on the other question.

bye,
--Nasser

Subject: permute/reshape

From: Roger Stafford

Date: 17 May, 2011 06:25:03

Message: 17 of 21

"Jane" wrote in message <iqsvan$1a3$1@newscl01ah.mathworks.com>...
> Hi, I couldn't get it to work for my data. Not sure if I gave the correct example. How do I get B back to A for the following? Just to test again.
>
> A=[1 2 3;
> 7 8 9;
> 13 14 15;
> 19 20 21;
> 25 26 27;
> 31 32 33;
> 37 38 39;
> 43 44 45;
> 49 50 51];
>
> B=[1 19 37 2 20 38 3 21 39; 7 25 43 8 26 44 9 27 45; 13 31 49 14 32 50 15 33 51];
>
> Thanks
- - - - - - - - - -
  You've changed the problem, Jane. Now it is much easier. You wanted a 3 by 9 matrix for A in the original problem but in this second version you are asking for a 9 by 3 result with different ordering. The new version takes only a single 'reshape' call.

  Are you sure you haven't made an error in this latest request. As it stands it is a very trivial task which you could easily have thought through by yourself.

 A2 = reshape(B,9,3);

Roger Stafford

Subject: permute/reshape

From: Nasser M. Abbasi

Date: 17 May, 2011 06:28:06

Message: 18 of 21

On 5/16/2011 11:14 PM, Nasser M. Abbasi wrote:

> ---------------------------------------------
> nRow = size(A,2); nCol=size(A,1);
> B = reshape( [A(:,1:nRow/2) A(:,nRow/2+1:nRow) ], nRow*nCol/2 ,2 )
> -------------------------------------------------
>
> This will give same output.
>

Even thought the above works, I missed up the names last minute making
a change. Here is how it should be:

---------------------------------------------
nRow=size(A,1); nCol=size(A,2);
B=reshape( [A(:,1:nCol/2) A(:,nCol/2+1:nCol)],nRow*nCol/2,2);
------------------------------------------------

--Nasser

Subject: permute/reshape

From: Nasser M. Abbasi

Date: 17 May, 2011 06:31:00

Message: 19 of 21


> Thank you Nasser. That code gives B matrix from A. How can I do that in reverse.
> Output A. B is known.

To get back, do

nRow=size(A,1); nCol=size(A,2);
reshape(B,nRow,nCol)

ans =

      1 2 3 4 5 6
      7 8 9 10 11 12
     13 14 15 16 17 18
     19 20 21 22 23 24
     25 26 27 28 29 30
     31 32 33 34 35 36
     37 38 39 40 41 42
     43 44 45 46 47 48
     49 50 51 52 53 54

--Nasser

Subject: permute/reshape

From: Roger Stafford

Date: 17 May, 2011 07:08:02

Message: 20 of 21

"Jane" wrote in message <iqt0pj$5fa$1@newscl01ah.mathworks.com>...
> Please help me Convert this B to A matrix. I believe this is what I need.
>
> A=[1 2 3 4 5 6;
> 7 8 9 10 11 12;
> 13 14 15 16 17 18;
> 19 20 21 22 23 24;
> 25 26 27 28 29 30;
> 31 32 33 34 35 36;
> 37 38 39 40 41 42;
> 43 44 45 46 47 48;
> 49 50 51 52 53 54];
>
> B=[1 4;
> 7 10;
> 13 16;
> 19 22;
> 25 28;
> 31 34;
> 37 40;
> 43 46;
> 49 52;
> 2 5;
> 8 11;
> 14 17;
> 20 23;
> 26 29;
> 32 35;
> 38 41;
> 44 47;
> 50 53;
> 3 6;
> 9 12;
> 15 18;
> 21 24;
> 27 30;
> 33 36;
> 39 42;
> 45 48;
> 51 54]';
- - - - - - - - - - -
  You can also do that with

 A2 = reshape(reshape(B,18,3).',6,9).';

It should be obvious how you can reverse this processing. Both 'reshape' and the transpose are entirely reversible.

Roger Stafford

Subject: permute/reshape

From: Steven_Lord

Date: 17 May, 2011 14:01:27

Message: 21 of 21



"Jane " <Jane@mytrashmail.com> wrote in message
news:iqsnhc$9bt$1@newscl01ah.mathworks.com...
> A=[1 2 3 4 5 6 7 8 9 ; 10 11 12 13 14 15 16 17 18 ; 19 20 21 22 23 24 25
> 26 27];
> B=[1 4 7 ; 10 13 16 ; 19 22 25 ; 2 5 8 ; 11 14 17 ; 20 23 26; 3 6 9; 12 15
> 18; 21 24 27]';
>
> Put B numbers back in the same order as A. Hope this makes sense.

A2 = reshape(B.', [3 9]);
isequal(A, A2)

B2 = reshape(A, [9 3]).';
isequal(B, B2)

--
Steve Lord
slord@mathworks.com
To contact Technical Support use the Contact Us link on
http://www.mathworks.com

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