3-dimensional array

16 次查看(过去 30 天)
yen
yen 2011-5-18
编辑: Stephen23 2025-3-5,18:06
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,15:45
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,18:03
编辑:Stephen23 2025-3-5,18:06
"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