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 2D 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 oneliner:
>
> 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
