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:MixedResidency | gpuArray and host inputs are mixed in one cat call. | cat: cannot mix gpuArray inputs with host arrays; convert them first |
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.- All inputs must be the same class (double, logical, complex, char, string, or cell).
- 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. - Empty inputs participate naturally—if any dimension is zero, the result is empty.
gpuArrayinputs stay on the device when an acceleration provider is registered.- Append an optional
'like', prototypepair to request output that matches the prototype's device residency; numeric prototypes may be host tensors orgpuArrayhandles, while logical prototypes must remain on the CPU.
Does RunMat run cat on the GPU?
When every input is a gpuArray, RunMat first calls the active provider's AccelProvider::cat hook to concatenate the buffers directly on the device. Providers that do not expose this hook trigger a transparent fallback: the operands are gathered to the host, concatenated with the same rules as CPU arrays, and uploaded back to the originating device so downstream work still sees a gpuArray. The 'like' prototype is honoured during fallback, ensuring the final residency matches your request. Mixing host arrays with gpuArray inputs is not supported—convert explicitly with gpuArray / gather to control 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
circshift · diag · flip · fliplr · flipud · horzcat · ipermute · kron · permute · repelem · repmat · reshape · rot90 · squeeze · tril · triu · vertcat
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.