cat
Joins arrays along a chosen dimension.
Category: Language Fundamentals > Matrices and Arrays
Syntax
C = cat(dim, A, B, ...)
Concatenates the inputs along dimension dim.
Description
cat(1, A, B) is the same as [A; B] and cat(2, A, B) the same as [A, B]. Its real use is dim 3 or higher, which brackets cannot express, for example to stack matrices into a 3-D array.
Cell arrays, strings and other array types can be joined too. If the other dimensions do not match, the call fails with a dimension error.
Input arguments
dim- Positive integer: 1 stacks vertically, 2 side by side, 3 along pages.
A, B, ...- Arrays of the same class (or compatible classes). All sizes must agree except along
dim.
Output arguments
C- The joined array. Its size along
dimis the sum of the input sizes.
Example
Stack two matrices into pages
A = [1 2; 3 4];
B = [5 6; 7 8];
C = cat(3, A, B);
size(C)
C(:, :, 2)
Output
ans = 1×3
2 2 2
ans = 2×2
5 6
7 8