MATLAB: How to reshape a matrix

reshape a matrix

Hello,
For example I would like to reshape the following matrix
1 0 0 2 0 0
0 1 0 0 2 0
to
1 0 0
0 1 0
2 0 0
0 2 0
The number of horizontally stacked matrices is now 2,so the solution is easy b = [a(1:2,:); a(3:end,:)];
The question is now how to do this for n horizontally stacked matrices without using a loop? Does someone know?
Kind regards, Carlas

Best Answer

A = [1 0 0 2 0 0; 0 1 0 0 2 0];
B = reshape(permute(reshape(A, 2, 3, 2), [1, 3, 2]), 4, 3)