Asked by Jan
on 13 May 2013

Hi all, can anyone help me with reshaping 3D matrix to 2D in way shown below? Lets say I have a 3D matrix A (A = round(rand(3,3,3)*10))

A(:,:,1) =

0 8 7 1 9 4 1 3 5

A(:,:,2) =

7 5 5 10 7 7 8 4 0

A(:,:,3) =

2 7 4 5 8 8 2 4 6

And i want a 2D matrix C C =

0 8 7 1 9 4 1 3 5 7 5 5 10 7 7 8 4 0 2 7 4 5 8 8 2 4 6

Thank you for any input for this problem

*No products are associated with this question.*

Answer by James Tursa
on 13 May 2013

Edited by James Tursa
on 13 May 2013

Accepted answer

For your specific example,

reshape(permute(A,[1 3 2]),9,3)

Alter the last two dimensions to fit your actual problem. The purpose of the permute in the above expression is to get the 3rd dimension next to the 1st dimension in memory, and then a simple reshape will finish the job.

Andrei Bobrov
on 14 May 2013

C = reshape(permute(A,[2 1 3]),3,[])'

A = permute(reshape(C,3,3,[]),[1 3 2])

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi test

Learn moreOpportunities for recent engineering grads.

Apply Today
## 0 Comments