MATLAB: How to get a 2D matrix out of the 3D matrix

1dimensionMATLABmatrixonesinglesingularthreetwo

I have a 2x100x10 matrix, "M", and I want to create two 2D matrices each 100×10 from them. When I try and do this with indexing, I get a 1x100x10 matrix that is still 3D, instead of the 100×10 2D matrix.
How do I get the desired matrices?
>> M = rand(2,100,10);
>> m1 = M(1,:,:);
>> m2 = M(2,:,:);
>> size(m1)
ans =
1 100 10

Best Answer

The last dimension (which now has a size of one) can be removed using the "squeeze" function. This function removes singleton dimensions.
>> M = rand(2,100,10);
>> m1 = squeeze(M(1,:,:));
>> m2 = squeeze(M(2,:,:));
>> size(m1)
ans =
100 10
For more information on the "squeeze" function, please see the following link: