cat — Concatenate arrays along a chosen dimension in MATLAB and RunMat.
cat(dim, A1, A2, ...) concatenates arrays along dimension dim, producing a new array whose slices along that dimension come from the inputs. Non-concatenated dimensions must be size-compatible, and ordering follows MATLAB and RunMat column-major semantics.
Syntax
B = cat(dim, A1, A2, An...)
B = cat(dim, A1, A2, An..., "like", prototype)Inputs
| Name | Type | Required | Default | Description |
|---|---|---|---|---|
dim | NumericScalar | Yes | — | Concatenation dimension (1-based). |
A1 | Any | Yes | — | First input array. |
A2 | Any | Yes | — | Second input array. |
An | Any | Variadic | — | Additional input arrays. |
name | PropertyName | Yes | "like" | Name-value key ('like'). |
prototype | LikePrototype | Yes | — | Prototype that sets output class/device. |
Returns
| Name | Type | Description |
|---|---|---|
B | Any | Concatenated output array. |
Errors
| Identifier | When | Message |
|---|---|---|
RunMat:cat:TooFewInputs | Fewer than two input arrays are supplied after dim. | cat: at least two input arrays are required |
RunMat:cat:InvalidDimension | dim is non-numeric, non-integer, or less than one. | cat: dimension must be numeric and >= 1 |
RunMat:cat:GpuError | A required gpuArray gather, upload, or provider operation fails. | cat: gpuArray operation failed |
RunMat:cat:TypeMismatch | Inputs are from incompatible classes for concatenation. | cat: incompatible input classes for concatenation |
RunMat:cat:InvalidLikePrototype | The 'like' name-value form is malformed or uses an unsupported prototype. | cat: invalid 'like' prototype |
How cat works
dimis 1-based and must be a positive integer.- Valid unlike-class combinations follow concatenation precedence: string dominates real numeric, character, and logical data; character dominates real numeric data but cannot combine with logical; an integer operand makes the leftmost nonempty integer class dominate real floating and logical operands; otherwise single dominates double, and logical is preserved only when every input is logical.
- Every ordered mixture of the eight real integer classes is accepted. When a nonempty input remains, empty operands are omitted before class selection; otherwise the leftmost integer class determines output class. Other numeric or logical values convert with ordinary rounding and saturation.
- Dimensions other than
dimmust match exactly; missing higher dimensions are treated as size1automatically. - The size along
dimbecomes the sum of the corresponding sizes from each input. - An empty operand is omitted when a nonempty compatible partner remains; compatible all-empty inputs retain the concatenated empty geometry.
gpuArrayinputs stay on the device when an acceleration provider is registered, and real or complex numeric host inputs may be mixed with resident inputs in the same call. Paired complex-integer data retains its exact component class and owning provider.- RunMat compatibility mode adds an optional trailing
"like", prototypepair that can request host or GPU output residency; this pair is not part of the pinned public MATLAB syntax.
Does RunMat run cat on the GPU?
When every supported floating input is a gpuArray, RunMat first calls the active provider's AccelProvider::cat hook. Typed integer data, mixed numeric host/device inputs, runtime empty omission, or providers without the hook use an exact fallback: resident operands are gathered without changing class, empty operands are omitted before class selection when a nonempty input remains, all remaining operands are concatenated on the host in original order, and the result is uploaded to the originating provider. This preserves integer class selection and wide values across the backend boundary. RunMat compatibility mode can additionally use the trailing "like" extension to request output residency.
Examples
Concatenating matrices by stacking rows
A = [1 2; 3 4];
B = [5 6; 7 8];
C = cat(1, A, B)Expected output:
C =
1 2
3 4
5 6
7 8Concatenating matrices by appending columns
left = [1 3; 2 4];
right = [10 30; 20 40];
wide = cat(2, left, right)Expected output:
wide =
1 3 10 30
2 4 20 40Building a 3-D array from 2-D slices
slice1 = magic(3);
slice2 = eye(3);
cube = cat(3, slice1, slice2)Concatenating logical masks without type changes
row = logical([1 0 1]);
mask = cat(1, row, ~row)Expected output:
mask =
1 0 1
0 1 0Joining character arrays into wider text rows
lhs = ['Run' ; 'GPU'];
rhs = ['Mat'; 'Fun'];
words = cat(2, lhs, rhs)Expected output:
words =
RunMat
GPUFunConcatenating string arrays along rows
names = ["alpha" "beta"];
more = ["gamma" "delta"];
combined = cat(1, names, more)Appending cell array columns for table-like data
cols1 = {1, 2; 'a', 'b'};
cols2 = {3, 4; 'c', 'd'};
tableCells = cat(2, cols1, cols2)Keeping gpuArray inputs on the device
G1 = gpuArray(rand(256, 256));
G2 = gpuArray(rand(256, 256));
stacked = cat(3, G1, G2)Requesting GPU output with the "like" prototype
G = gpuArray(rand(3, 3));
H = cat(3, zeros(3, 3), ones(3, 3), "like", G)Concatenating complex arrays preserves imaginary parts
z1 = complex([1 2], [3 4]);
z2 = complex([5 6], [7 8]);
joined = cat(2, z1, z2)Combining empty inputs yields an empty result
emptyRow = zeros(0, 3);
combo = cat(1, emptyRow, emptyRow)Using cat with coding agents
Open a RunMat example with live inputs, then ask the agent to explain how cat changes the result.
Run a small cat example, explain the result, then change one input and compare the output.
Related Array functions
Shape
blkdiag · circshift · diag · flip · fliplr · flipud · horzcat · ipermute · kron · permute · repelem · repmat · reshape · rot90 · squeeze · toeplitz · tril · triu · vertcat
Grouping
accumarray · combinations · discretize · findgroups · groupcounts · grp2idx · splitapply
Sorting Sets
argsort · intersect · ismember · ismembertol · issorted · issortedrows · setdiff · setxor · sort · sortrows · union · unique
Open-source implementation
Unlike proprietary runtimes, every RunMat function is open-source. Read exactly how cat is executed, line by line, in Rust.
- View the source for cat in Rust on GitHub
- Learn how the RunMat runtime works
- Found a bug? Open an issue with a minimal reproduction.
About RunMat
RunMat is an open-source runtime that executes MATLAB-syntax code blazing on any GPU. It is licensed under the Apache 2.0 license.
- RunMat automatically optimizes your math for GPU execution on Apple, Nvidia, and AMD hardware. No code changes needed. Simulations that took hours now take minutes.
- Start running code in seconds. RunMat runs in the browser, on the desktop, or from the CLI. No license server, no IT ticket.