MATLAB: Section a 2d matrix and put the sections into a 3d matrix

rearrange matrix

Hi,
I have a question, if I have a matrix, lets say a 3×12 matrix:
a=
1 1 1
1 1 1
1 1 1
1 1 1
2 2 2
2 2 2
2 2 2
2 2 2
3 3 3
3 3 3
3 3 3
3 3 3
is there a method other than for looping to section this matrix and place the elements in a 3d matrix such, that it has a structure of (without changing the position of the 1, 2, 3 elements):
b(:,:,1) =
1 1 1
1 1 1
1 1 1
1 1 1
b(:,:,2) =
2 2 2
2 2 2
2 2 2
2 2 2
b(:,:,3) =
3 3 3
3 3 3
3 3 3
3 3 3
Is there a method doing this without using for looping?

Best Answer

Just repeating my comment as an answer, in case you want to credit it:
b = permute(reshape(a',[3 3 3]), [2 1 3]);
if starting with a0:
b = permute(reshape(a0', [3 3 3], [3 2 1]);