3-dimensional array

조회 수: 9 (최근 30일)
yen
yen 2011년 5월 18일
편집: Stephen23 2025년 3월 5일
Example a=[1 0;0 1] b=[2 2;2 2] c=[0 3;3 0] d=cat(3,a,b,c) ==> d(:,:,1)=a ; d(:,:,2)=b; d(:,:,3)=c.
I have n-dimensional arrays are the same size, I want to merge into a 3-dimensional array. I have known about the "cat" function to concatenates into 3-dimensional array such as example , but not understand much about how to use it to solve my problem
  댓글 수: 2
Andrew Newell
Andrew Newell 2011년 5월 18일
Can n be greater than 3?
Oleg Komarov
Oleg Komarov 2011년 5월 18일
@yen: Do you have a question?

댓글을 달려면 로그인하십시오.

답변 (1개)

Leepakshi
Leepakshi 2025년 3월 5일
Hi Yen,
To merge multiple n-dimensional arrays of identical size into a single 3-dimensional array, the cat function in MATLAB can be utilized. This function concatenates arrays along a specified dimension.
For example, given 2D arrays a, b, and c, the command cat(3, a, b, c) will concatenate these arrays along the third dimension, resulting in a 3D array. Each slice of the 3D array corresponds to one of the original arrays.
% Define the 2D arrays
a = [1 0; 0 1];
b = [2 2; 2 2];
c = [0 3; 3 0];
% Concatenate them into a 3D array
d = cat(3, a, b, c);
% Display the result
disp('d(:,:,1) =');
disp(d(:,:,1));
disp('d(:,:,2) =');
disp(d(:,:,2));
disp('d(:,:,3) =');
disp(d(:,:,3));
Please refer to below documentation on the cat function for more clarity:
Hope this helps!
  댓글 수: 1
Stephen23
Stephen23 2025년 3월 5일
편집: Stephen23 2025년 3월 5일
"To merge multiple n-dimensional arrays of identical size into a single 3-dimensional array, the cat function in MATLAB can be utilized."
Not in general, CAT can't be utilized alone for this purpose. Here is a simple counter-example with three "n-dimensional arrays of identical size" and the output is definitely not a "single 3-dimensional array":
a = rand(5,4,3,2);
b = rand(5,4,3,2);
c = rand(5,4,3,2);
d = cat(3,a,b,c);
size(d) % not a "3-dimensional array"
ans = 1×4
5 4 9 2
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>

댓글을 달려면 로그인하십시오.

Community Treasure Hunt

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

Start Hunting!

Translated by