Creating a 3D array from 2D array

15 ビュー (過去 30 日間)
sermet OGUTCU
sermet OGUTCU 2021 年 11 月 3 日
編集済み: Dave B 2021 年 11 月 3 日
x = 121 x 27
y = 121 x 27
z = 121 x 27
I need to create a 3D array from these 2D arrays as following:
xyz= 121 x 3 x 27
where rows equal 121 ; columns equal 3 ( x(1,1) y(1,1) z(1,1) , x(2,1) y(2,1) z(2,1), ... x(n,1) y(n,1) z(n,1), x(1,2) y(1,2) z(1,2), ... x(1,n) y(1,n) z(1,n) ) ; slices equal 27.

採用された回答

Dave B
Dave B 2021 年 11 月 3 日
You can use the cat command for this:
x=rand(121,27);
y=rand(121,27);
z=rand(121,27);
xyz=cat(3,x,y,z);
size(xyz)
ans = 1×3
121 27 3
Or you can initialize a 3-D matrix and stick your 2-D matrices in:
xyz2=nan([size(x) 3]);
xyz2(:,:,1)=x;
xyz2(:,:,2)=y;
xyz2(:,:,3)=z;
isequal(xyz,xyz2) % just to confirm that both methods do the same thing
ans = logical
1
  3 件のコメント
Dave B
Dave B 2021 年 11 月 3 日
編集済み: Dave B 2021 年 11 月 3 日
Sorry I missed the bit about the order, if you don't want to do the permute bit you can alter the second method to:
x=rand(121,27);
y=rand(121,27);
z=rand(121,27);
xyz = nan([size(x,1) 3 size(x,2)]);
xyz(:,1,:)=x;
xyz(:,2,:)=y;
xyz(:,3,:)=z;
size(xyz)
ans = 1×3
121 3 27
sermet OGUTCU
sermet OGUTCU 2021 年 11 月 3 日
Dear @Dave B,
Thanks a lot for the solution.

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

その他の回答 (0 件)

カテゴリ

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

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by