How can I get 2D Matrices from a 3D matrix?

I have looked for someone with my same question but I haven't found nothing so specific.

I have a 3D matrix ( m x n x z ) and I want to get from that Matrix, the 2D matrices m x z and n x z. I am working with 3D medical images and I need different views of my patient; I haven´t any problem to get the m x n matrix with this code:

 for z=1:length(z)
 figure;
 imshow (A (:,:,z))
 end

but if I do the same with the other variables, my resultant matrix is still a 3D matrix and I can't show it with the "imshow" function.

Please, could you help me?

Thank you so much.

Upvotes: 0

Views: 502

Answers (1)

Luis Mendo
Luis Mendo

Reputation: 112659

Use squeeze to remove singleton dimensions. Example:

k = 1; %// or any desired value from 1 to n
imshow(squeeze(A(:,k,:)))

How this works: A(:,k,:) has size mx1xz (3D array). squeeze removes that singleton dimension, so that squeeze(A(:,k,:)) has size mxz (2D array).

Upvotes: 1

Related Questions