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
| Name | Type | Required | Default | Description |
|---|---|---|---|---|
A | Any | Yes | — | Input array/value. |
sz | NumericArray | Yes | — | Row/column size vector. May include one auto-sized dimension ([]). |
m | NumericScalar | Yes | — | First output dimension size (or [] for auto). |
n | NumericScalar | Yes | — | Second output dimension size (or [] for auto). |
p | NumericScalar | Variadic | — | Additional output dimension sizes (or [] for auto). |
Returns
| Name | Type | Description |
|---|---|---|
B | Any | Input array data with reshaped dimensions. |
Errors
| Identifier | When | Message |
|---|---|---|
RunMat:reshape:SizeMissing | No size vector/scalars are provided after A. | reshape: size information missing |
RunMat:reshape:InvalidSize | Size arguments are malformed, non-scalar where scalar required, or contain invalid values. | reshape: invalid size specification |
RunMat:reshape:UnsupportedInput | Input value type is unsupported for reshape. | reshape: unsupported input type |
How reshape works
- Accepts either a size vector
reshape(A, [m n …])or individual dimensionsreshape(A, m, n, …). - Exactly one dimension may be specified as
[]; RunMat infers its value fromnumel(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 scalar5, 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 12Using 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 0Reshaping 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.
Related Array functions
Open-source implementation
Unlike proprietary runtimes, every RunMat function is open-source. Read exactly how reshape is executed, line by line, in Rust.
- View the source for reshape 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.