MATLAB: How to concatenate a 3-d matrix to 2-d

3-d matrixindexing

Suppose I have a 3-d matrix A. I want to reshape it to a 2-d matrix B as follow:
B=[A(:,:,1); A(:,:,2); . . .; A(:,:,end)];
Is there some easy way to do it?

Best Answer

Two methods, I'll let you test which one is faster, probably the permute one:
B = reshape(permute(A, [1 3 2]), [], size(A, 2))
B = num2cell(A, [1 2]);
B = vertcat(B{:})