1

I am trying to turn a 3 dimensional array "upside-down" as:

I have tried the inverse function, but if we look at the inverse operation in mathematical terms, it gives us another result. I need to turn without changing the data in the array. How to do that?

To split the 3-dimensional array (A x B x C) into A-sub-array (2d, B x C) I have used squeeze: k=squeeze(array(n,:,:)). Now I have a 2-dimensional array of size B x C. How to put it back together (in 3 dimensional array)?

enter image description here

Adriaan
  • 17,741
  • 7
  • 42
  • 75

1 Answers1

1

You can use permute() to change the order of dimensions, which can be used as a multidimensional transpose.

Putting 2D matrices into a 3D one then is a simple indexing operation. Read more in indexing here.

A = rand(10,10,10);
B = permute(A, [ 3 2 1 ]);  % Permute he order of dimensions

mat1 = rand(10,10);
mat2 = rand(10,10);
mat_both(:,:,2) = mat2; % Stack 2D matrices along the third dimension
mat_both(:,:,1) = mat1;

mat_both = cat(3,mat1, mat2);  % Stacks along the third dimension in a faster way
Adriaan
  • 17,741
  • 7
  • 42
  • 75