RunMat
GitHub

reshape — Reshape arrays to new dimensions without changing element order or values.

reshape(A, newSize) returns the elements of A with a different dimensional layout while preserving MATLAB column-major element order. The total number of elements must remain unchanged.

Syntax

B = reshape(A, sz)
B = reshape(A, m, n)
B = reshape(A, m, n, p...)

Inputs

NameTypeRequiredDefaultDescription
AAnyYesInput array/value.
szNumericArrayYesRow/column size vector. May include one auto-sized dimension ([]).
mNumericScalarYesFirst output dimension size (or [] for auto).
nNumericScalarYesSecond output dimension size (or [] for auto).
pNumericScalarVariadicAdditional output dimension sizes (or [] for auto).

Returns

NameTypeDescription
BAnyInput array data with reshaped dimensions.

Errors

IdentifierWhenMessage
RunMat:reshape:SizeMissingNo size vector/scalars are provided after A.reshape: size information missing
RunMat:reshape:InvalidSizeSize arguments are malformed, non-scalar where scalar required, or contain invalid values.reshape: invalid size specification
RunMat:reshape:UnsupportedInputInput value type is unsupported for reshape.reshape: unsupported input type

How reshape works

  • Accepts either a size vector reshape(A, [m n …]) or individual dimensions reshape(A, m, n, …).
  • Exactly one dimension may be specified as []; RunMat infers its value from numel(A).
  • Dimensions must be nonnegative integers. Zero-sized dimensions are allowed when numel(A) == 0.
  • Works on numeric, logical, complex, string, char, GPU, and cell arrays (cell/char currently support up to 2-D).
  • Reshaping never copies data; it only reinterprets layout metadata.
  • Scalar inputs follow MATLAB semantics: reshape(5, 1, 1) yields the scalar 5, while larger shapes return dense arrays.

Does RunMat run reshape on the GPU?

When the input lives on the GPU, RunMat asks the active acceleration provider to apply the reshape hook so the backend can update its residency metadata. No data transfers or kernel launches are needed, so gpuArray inputs stay on the device. Providers that do not override the hook fall back to updating the tensor handle directly, which is sufficient for the in-process reference backend.

Examples

Reshaping a row vector into a matrix

A = 1:12;
B = reshape(A, [3, 4])

Expected output:

B =
    1     4     7    10
    2     5     8    11
    3     6     9    12

Using an automatically inferred dimension

A = 1:18;
B = reshape(A, 3, [])

Expected output:

size(B)  % => [3 6]

Reshaping into three dimensions

A = 1:24;
C = reshape(A, [2, 3, 4])

Expected output:

size(C)  % => [2 3 4]

Reshaping logical arrays preserves type

mask = logical([1 0 1 0 1 0]);
grid = reshape(mask, 2, 3)

Expected output:

grid =
     1     1     1
     0     0     0

Reshaping GPU data without gathering

G = gpuArray(1:1000);
H = reshape(G, 10, 100)

Handling zero-sized dimensions

E = reshape([], 0, 3)

Expected output:

size(E)  % => [0 3]

Using reshape with coding agents

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

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

Shape

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

Sorting Sets

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

Creation

colon · eye · false · fill · linspace · logspace · magic · meshgrid · 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 reshape 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.