MATLAB: Can anyone help me with making a 2D matrix into a 3D matrix

help please

I have a (4 x 200) 2d matrix and I need to make a 3d matrix of dimensions (4 x 4 x 50) I need to take 4 rows in 2d matrix and 4 columns of that but my columns repitation of 50. for example : for (4 x 200) 2d matrix if I choose 1th of column in 2d matrix for 1th column in 3d matrix, 2th column in 3d matrix is 51th column of 2d matrix and respectivily; 3th –> 101th , 4th–> 151th. this 4 columns are my page1 in 3d matrix and for the other pages as the same way. but how can I do this?

Best Answer

Such problems can be solved in general by:
Y = reshape(permute(reshape(X, size1), order), size2)
Here:
a = [1 2 3 4 5 6; 7 8 9 10 11 12; 13 14 15 16 17 18; 19 20 21 22 23 24];
b = reshape(permute(reshape(a, 4, 2, 3), [1,3,2]), 4, 3, [])
Finding the correct sizes and order is not easy, but not magic: The 4 initial elements in the first column should be kept together, but the neighboring columns should be reordered, so they must be separated:
reshape(a, 4, 2, 3)
These columns should appear in the 3rd dimension, so use permute to move them there:
permute(..., [1, 3, 2])
3rd and 2nd dimension are swapped. Finally the next reshape is not not needed in your example, so this is enough:
b = permute(reshape(a, 4, 2, 3), [1,3,2]))