How to: taking out 2d matrix from 3d matrix

36 visualizaciones (últimos 30 días)
adi kul
adi kul el 19 de Mayo de 2016
Comentada: adi kul el 19 de Mayo de 2016
Hello All, I need help in 3d matrix. I have z as 2x20x30 matrix which I will represnt here as:
z(i,m,n)
Now I want to take out z1 as (m,n) and z2 as (m,n) matrix from this. Meaning the i from z(i,m,n) is i=1:2. So z1 will be (m,n) when i=1 and z2 will be (m,n) when i=2
What I did is:
z1(m,n)=z(1,m,n)
z2(m,n)=z(2,m,n)
But this yields only 1 value and rest of z1 matrix values turns out as zero.
I am not sure how to pull this out. Can anyone help me? I hope you understand my problem. Regards, Adwait

Respuesta aceptada

Guillaume
Guillaume el 19 de Mayo de 2016
Azzi showed you one way to do it. You could also do
z1 = squeeze(z(1, :, :));
z2 = squeeze(z(2, :, :));
the permute beforehand has the advantage you don't need to squeeze out the singleton dimension.
But more importantly, there is no reason for you to split out your matrix anyway. Whenever in your later code, you use z1 you could simply use z(1, :, :) and whenever you use z2 you simply use z(2, :, :). As a rule, you shouldn't be numbering variables. If the variables are numbered it really means that their content should be put together in array, which is what you started with.

Más respuestas (1)

Azzi Abdelmalek
Azzi Abdelmalek el 19 de Mayo de 2016
z=randi(4,4,3,2)
A=permute(z,[2 3 1])
z1=A(:,:,1)
z2=A(:,:,2)
  1 comentario
adi kul
adi kul el 19 de Mayo de 2016
Thank you for your answer. It took me a while to understand this but I preferred Guillaume's answer as it's easy and exactly what I wanted.
Thank you again.

Iniciar sesión para comentar.

Categorías

Más información sobre Shifting and Sorting Matrices en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by