Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Matlab reshape horizontal cat

Tags:

arrays

matlab

Hi I want to reshape a matrix but the reshape command doesn't order the elements the way I want it. I have matrix with elements:

A B
C D
E F
G H
I K
L M

and want to reshape it to:

A B E F I K
C D G H L M

So I know how many rows I want to have (in this case 2) and all "groups" of 2 rows should get appended horizontally. Can this be done without a for loop?

like image 862
Stefan Avatar asked Jun 26 '14 09:06

Stefan


1 Answers

You can do it with two reshape and one permute. Let n denote the number of rows per group:

y = reshape(permute(reshape(x.',size(x,2),n,[]),[2 1 3]),n,[]);

Example with 3 columns, n=2:

>> x = [1 2 3; 4 5 6; 7 8 9; 10 11 12]
x =
     1     2     3
     4     5     6
     7     8     9
    10    11    12
>> y = reshape(permute(reshape(x.',size(x,2),n,[]),[2 1 3]),n,[])
y =
     1     2     3     7     8     9
     4     5     6    10    11    12
like image 101
Luis Mendo Avatar answered Sep 18 '22 17:09

Luis Mendo