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:
Reformat a matrix (shift/interchange columns)

Subject: Reformat a matrix (shift/interchange columns)

From: Karthik Ramanathan

Date: 12 May, 2004 18:43:34

Message: 1 of 4

I have a matrix A of n*n. For a given value 'm' (m < n, and n/m = integer
i.e., m divides n) I want to rearrange the matrix A in the following format:

Let:
A = [A1 A2 .... An], where A1, A2, .. An are all column vectors

the changed matrix should be B = [A1 A(m+1) A(2*m+1)... A(2) A(m+2) A(2*m+2)
... A(m) A(2*m) ...]

lets for example take n = 10 and m = 2

A = [A1 A2 A3 A4 A5 A6 A7 A8 A9 A10];

B = [A1 A3 A5 A7 A9 A2 A4 A6 A8 A10];

if n = 9, m = 3

A = [A1 A2 A3 A4 A5 A6 A7 A8 A9 ];

B = [A1 A4 A7 A2 A5 A8 A3 A6 A9 ];

I am currently doing this in a for loop:

A= rand(n);
B=[];
for j = 1:m
B = [B A(:,j:m:end)];
end

Is there a more efficient way which will work for large values of m, n?

Subject: Reformat a matrix (shift/interchange columns)

From: Giuseppe RidinĂ²

Date: 13 May, 2004 02:57:43

Message: 2 of 4

Karthik Ramanathan wrote:
>
>
> I have a matrix A of n*n. For a given value 'm' (m < n, and n/m
=
> integer
> i.e., m divides n) I want to rearrange the matrix A in the
> following format:
>
> Let:
> A = [A1 A2 .... An], where A1, A2, .. An are all column vectors
>
> the changed matrix should be B = [A1 A(m+1) A(2*m+1)... A(2) A(m+2)
> A(2*m+2)
> ... A(m) A(2*m) ...]
>
> lets for example take n = 10 and m = 2
>
> A = [A1 A2 A3 A4 A5 A6 A7 A8 A9 A10];
>
> B = [A1 A3 A5 A7 A9 A2 A4 A6 A8 A10];
>
> if n = 9, m = 3
>
> A = [A1 A2 A3 A4 A5 A6 A7 A8 A9 ];
>
> B = [A1 A4 A7 A2 A5 A8 A3 A6 A9 ];
>
> I am currently doing this in a for loop:
>
> A= rand(n);
> B=[];
> for j = 1:m
> B = [B A(:,j:m:end)];
> end
>
> Is there a more efficient way which will work for large values of
> m, n?
>
>

Try this:

index = reshape(1:n,m,n/m)';
index = index(:);
B = A(:,index);

Subject: Reformat a matrix (shift/interchange columns)

From: Peter Boettcher

Date: 13 May, 2004 08:33:58

Message: 3 of 4

"Karthik Ramanathan" <rkarthik@nospam.softhome.net> writes:

> I have a matrix A of n*n. For a given value 'm' (m < n, and n/m = integer
> i.e., m divides n) I want to rearrange the matrix A in the following format:
>
> Let:
> A = [A1 A2 .... An], where A1, A2, .. An are all column vectors
>
> the changed matrix should be B = [A1 A(m+1) A(2*m+1)... A(2) A(m+2) A(2*m+2)
> ... A(m) A(2*m) ...]
>
> lets for example take n = 10 and m = 2
>
> A = [A1 A2 A3 A4 A5 A6 A7 A8 A9 A10];
>
> B = [A1 A3 A5 A7 A9 A2 A4 A6 A8 A10];
>
> if n = 9, m = 3
>
> A = [A1 A2 A3 A4 A5 A6 A7 A8 A9 ];
>
> B = [A1 A4 A7 A2 A5 A8 A3 A6 A9 ];
>
> I am currently doing this in a for loop:
>
> A= rand(n);
> B=[];
> for j = 1:m
> B = [B A(:,j:m:end)];
> end
>
> Is there a more efficient way which will work for large values of m, n?

Remember that if you read a matrix like a vector, the elements read
out columnwise. The 2-D version looks like this:

C = reshape(A, m, n/m).';
B = C(:);

Since you want to reorder vector inputs, you can use the same idea,
but with a 3D matrix:

C = permute(reshape(A, [n m n/m]), [1 3 2]);
B = reshape(C, [n n]);

Or paste it all together into one huge nasty one-liner:

B = reshape(permute(reshape(A, [n m n/m]), [1 3 2]), [n n]);


--
Peter Boettcher <boettcher@ll.mit.edu>
MIT Lincoln Laboratory
MATLAB FAQ: http://www.mit.edu/~pwb/cssm/

Subject: Reformat a matrix (shift/interchange columns)

From: Karthik

Date: 13 May, 2004 13:32:59

Message: 4 of 4

Peter Boettcher wrote:
>
>
> "Karthik Ramanathan" <rkarthik@nospam.softhome.net> writes:
>
>> I have a matrix A of n*n. For a given value 'm' (m < n, and
n/m =
> integer
>> i.e., m divides n) I want to rearrange the matrix A in the
> following format:
>>
>> Let:
>> A = [A1 A2 .... An], where A1, A2, .. An are all column vectors
>>
>> the changed matrix should be B = [A1 A(m+1) A(2*m+1)... A(2)
> A(m+2) A(2*m+2)
>> ... A(m) A(2*m) ...]
>>
>> lets for example take n = 10 and m = 2
>>
>> A = [A1 A2 A3 A4 A5 A6 A7 A8 A9 A10];
>>
>> B = [A1 A3 A5 A7 A9 A2 A4 A6 A8 A10];
>>
>> if n = 9, m = 3
>>
>> A = [A1 A2 A3 A4 A5 A6 A7 A8 A9 ];
>>
>> B = [A1 A4 A7 A2 A5 A8 A3 A6 A9 ];
>>
>> I am currently doing this in a for loop:
>>
>> A= rand(n);
>> B=[];
>> for j = 1:m
>> B = [B A(:,j:m:end)];
>> end
>>
>> Is there a more efficient way which will work for large values
of
> m, n?
>
> Remember that if you read a matrix like a vector, the elements read
> out columnwise. The 2-D version looks like this:
>
> C = reshape(A, m, n/m).';
> B = C(:);
>
> Since you want to reorder vector inputs, you can use the same idea,
> but with a 3D matrix:
>
> C = permute(reshape(A, [n m n/m]), [1 3 2]);
> B = reshape(C, [n n]);
>
> Or paste it all together into one huge nasty one-liner:
>
> B = reshape(permute(reshape(A, [n m n/m]), [1 3 2]), [n n]);
>
>
> --
> Peter Boettcher <boettcher@ll.mit.edu>
> MIT Lincoln Laboratory
> MATLAB FAQ: <http://www.mit.edu/~pwb/cssm/>
>

Thanks Peter for the help.

Karthik

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