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{:}) Related SolutionsMATLAB: Efficiently converting a 3d matrix to a 2d matrix You can permute the 2nd and 3rd dimension before doing the reshape: >> C = permute(A,[1 3 2]); >> C = reshape(C,[],size(A,2),1) MATLAB: Concatenate multi dimensional matrix Your b = cat(1, a(:, :, i)) is incorrect and is just equivalent to b = a(:, :, i). If you were to use a loop you could do it like this:b = [];for i = 1:size(a, 3) b = [b; a(:, :, 1)]; %which is cat(1, b, a(:, :, i))endThis would be extremely innefficient and matlab's editor will warn you that you're resizing b on every iteration. The proper way to do it in matlab is:b = reshape(permute(a, [1 3 2]), [], size(a, 2))without a loop. Related QuestionConcatenate arrays after performing additionHow to reshape this matrixVector to repeated matrix multiplicationHow to reshape a matrixHow to create a 3-d matrix from a 2-d matrixHow do you turn a 30x30x24 matrix into a 24×900 matrix
Best Answer