Change matrix from 3d to 2d rows

I will convert a 3d matrix to a 2d matrix. This is a form conversion: [nxmxo] → [n * oxm].

Matrix elements are connected row by row. Thus, the resulting matrix requires the same rows.

A = rand(2,2,3); 

Performing this action:

 C = reshape(A, 2*3, 2); 

does not save rows in A.

So, I am doing this:

 B = zeros(size(A,1)*size(A,3),size(A,2)); first_indice = 1; for i = 1:size(A,3) B(first_indice:size(A,1)*i,:)=A(:,:,i); first_indice = first_indice + size(A,1); end 

Is there a more efficient way, possibly using a shape change?

Thanks a lot!

+4
source share
1 answer

reshape combines matrix elements from the first dimension. So the solution would be to resize before changing. In your case, it should be as follows:

 % A is the input matrix of dimensions (nxmxo). B = ipermute(A, [1 3 2]); C = reshape(B, n*o, m); 
+4
source

Source: https://habr.com/ru/post/1388289/


All Articles