RunMat
GitHub

horzcat — Concatenate arrays horizontally in MATLAB and RunMat.

horzcat(A1, A2, ...) concatenates inputs along dimension 2, matching MATLAB square-bracket syntax [A1 A2 ...]. Size compatibility and type coercion follow MATLAB semantics.

Syntax

B = horzcat()
B = horzcat(A1, An...)

Inputs

NameTypeRequiredDefaultDescription
A1AnyYesFirst input array.
AnAnyVariadicAdditional input arrays.

Returns

NameTypeDescription
BAnyHorizontally concatenated result (dimension 2).

Errors

IdentifierWhenMessage
RunMat:horzcat:InvalidInputInputs are malformed or incompatible for horizontal concatenation.horzcat: invalid input arguments
RunMat:horzcat:TypeMismatchInput classes cannot be concatenated together.horzcat: incompatible input classes for concatenation

How horzcat works

  • Operates on numeric, logical, complex, character, string, and cell arrays with MATLAB-compatible type checking.
  • All inputs must have the same number of rows (dimension 1). Higher dimensions are padded with singleton sizes where necessary.
  • Scalars act as 1×1 building blocks, so horzcat(1, 2, 3) produces the row vector [1 2 3].
  • Empty inputs participate naturally. If every operand is empty, the result is the canonical 0×0 double.
  • When the trailing 'like', prototype pair is supplied, the output matches the prototype's residency (host or GPU) and numeric category.
  • Mixing gpuArray operands with host operands is an error—convert explicitly using gpuArray or gather.

Does RunMat run horzcat on the GPU?

horzcat delegates to cat(dim = 2, …). When the active acceleration provider implements the cat hook, the concatenation is executed directly on the GPU without staging data back to the CPU. Providers that lack this hook fall back to gathering the operands, concatenating on the host, and uploading the result so downstream code still sees a gpuArray. This mirrors MATLAB's explicit GPU workflow while keeping RunMat's auto-offload planner informed.

Examples

Concatenating matrices by columns

A = [1 2; 3 4];
B = [10 20; 30 40];
C = horzcat(A, B)

Expected output:

C =
     1     2    10    20
     3     4    30    40

Building a row vector from scalars

row = horzcat(1, 2, 3, 4)

Expected output:

row = [1 2 3 4]

Extending character arrays into words

lhs = ['Run' ; 'GPU'];
rhs = ['Mat' ; 'Fun'];
words = horzcat(lhs, rhs)

Expected output:

words =
    RunMat
    GPUFun

Keeping gpuArray inputs resident on the device

G1 = gpuArray(rand(256, 128));
G2 = gpuArray(rand(256, 64));
wide = horzcat(G1, G2)

Preserving empties when all inputs are empty

emptyBlock = zeros(0, 3);
result = horzcat(emptyBlock, emptyBlock)

Matching an output prototype with the 'like' syntax

proto = gpuArray.zeros(5, 1);
combo = horzcat(ones(5, 1), zeros(5, 1), "like", proto)

Using horzcat with coding agents

Open a RunMat example with live inputs, then ask the agent to explain how horzcat changes the result.

Run a small horzcat example, explain the result, then change one input and compare the output.

Shape

cat · circshift · diag · flip · fliplr · flipud · ipermute · kron · permute · repelem · repmat · reshape · rot90 · squeeze · tril · triu · vertcat

Sorting Sets

argsort · intersect · ismember · issorted · setdiff · sort · sortrows · union · unique

Creation

colon · eye · false · fill · inf · linspace · logspace · magic · meshgrid · nan · ones · peaks · rand · randi · randn · randperm · range · true · zeros

Indexing

find · ind2sub · sub2ind

Introspection

isempty · ismatrix · isscalar · isvector · length · ndims · numel · size

Open-source implementation

Unlike proprietary runtimes, every RunMat function is open-source. Read exactly how horzcat is executed, line by line, in Rust.

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.

Getting started · Benchmarks · Pricing

Download RunMat

Download RunMat for full performance, or use RunMat in your browser for zero setup.