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

34 ビュー (過去 30 日間)
adi kul
adi kul 2016 年 5 月 19 日
コメント済み: adi kul 2016 年 5 月 19 日
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

採用された回答

Guillaume
Guillaume 2016 年 5 月 19 日
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.
  1 件のコメント
adi kul
adi kul 2016 年 5 月 19 日
Thank you. This is what exactly I wanted.

サインインしてコメントする。

その他の回答 (1 件)

Azzi Abdelmalek
Azzi Abdelmalek 2016 年 5 月 19 日
z=randi(4,4,3,2)
A=permute(z,[2 3 1])
z1=A(:,:,1)
z2=A(:,:,2)
  1 件のコメント
adi kul
adi kul 2016 年 5 月 19 日
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.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeShifting and Sorting Matrices についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by