3-dimensional array

9 views (last 30 days)
yen
yen on 18 May 2011
Edited: Stephen23 on 5 Mar 2025
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 Comments
Andrew Newell
Andrew Newell on 18 May 2011
Can n be greater than 3?
Oleg Komarov
Oleg Komarov on 18 May 2011
@yen: Do you have a question?

Sign in to comment.

Answers (1)

Leepakshi
Leepakshi on 5 Mar 2025
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 Comment
Stephen23
Stephen23 on 5 Mar 2025
Edited: Stephen23 on 5 Mar 2025
"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>

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!