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
A- Type
Any- Required
- Yes
- Description
- Input array/value.
sz- Type
NumericArray- Required
- Yes
- Description
- Row/column size vector. May include one auto-sized dimension ([]).
m- Type
NumericScalar- Required
- Yes
- Description
- First output dimension size (or [] for auto).
n- Type
NumericScalar- Required
- Yes
- Description
- Second output dimension size (or [] for auto).
p- Type
NumericScalar- Required
- Variadic
- Description
- Additional output dimension sizes (or [] for auto).
Returns
B- Type
Any- Description
- Input array data with reshaped dimensions.
Errors
RunMat:reshape:SizeMissing- When
- No size vector/scalars are provided after A.
- Message
reshape: size information missing
RunMat:reshape:InvalidSize- When
- Size arguments are malformed, non-scalar where scalar required, or contain invalid values.
- Message
reshape: invalid size specification
RunMat:reshape:UnsupportedInput- When
- Input value type is unsupported for reshape.
- Message
reshape: unsupported input type
Behavior
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, cell, structure, and object arrays using each runtime carrier's admitted shape rules. Character and cell arrays can be reshaped into higher dimensions when the element count matches.
All eight integer data classes preserve authoritative native storage exactly, and typed integer dimensions are decoded structurally.
Contiguous GPU reshape retains the input allocation as a shape view. Lazy device layouts materialize through their exact owner; host carriers retain their existing structural reshape path.
Scalar input preserves its value; an output extent product other than one is rejected. Higher-rank singleton shapes may use an array carrier.
size(B) % => [3 6]
size(C) % => [2 3 4]
size(E) % => [0 3]
Execution and residency
Contiguous GPU shape views retain the authentic allocation without transfer. Changed lazy layouts materialize through the exact owning provider with output validation.
Structural data retains its admitted single, double, logical, or native signed/unsigned integer storage, including supported complex arrays. This does not upgrade preceding numerical accuracy or authorize an unsupported carrier.
The first argument is the data operand; remaining arguments, where present, are dimension or order controls. No scalar broadcasting, elementwise/reduction fusion template, or standalone fusion constant ABI is declared.
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, [])Reshaping into three dimensions
A = 1:24;
C = reshape(A, [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)Reshaping complex GPU data keeps complex residency
Z = gpuArray([1+2i 3+4i 5+6i]);
R = reshape(Z, 3, 1);
isgpuarray(R)Handling zero-sized dimensions
E = reshape([], 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
Shape
blkdiag · cat · circshift · diag · flip · fliplr · flipud · horzcat · ipermute · kron · permute · repelem · repmat · rot90 · shiftdim · squeeze · toeplitz · tril · triu · vertcat
Accumulation
Sorting Sets
argsort · intersect · ismember · ismembertol · issorted · issortedrows · setdiff · setxor · sort · sortrows · union · unique
Creation
colon · createArray · empty · eye · false · full · inf · int16.empty · int32.empty · int64.empty · int8.empty · linspace · logspace · magic · meshgrid · nan · ndgrid · nonzeros · ones · peaks · rand · randi · randn · randperm · range · sparse · spdiags · speye · spones · sprand · true · uint16.empty · uint32.empty · uint64.empty · uint8.empty · zeros
Combinatorics
Binning
Grouping
Source you can read
Every RunMat function's source is public. 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 a runtime that executes MATLAB-syntax code fast on any GPU. The runtime is free for individuals, including freelancers; students and faculty at an academic institution, for teaching or research there; contributors to an open-source project, for work on that project; organizations with 250 or fewer people and US$10M or less in annual revenue; and anyone, for non-production use. See runmat.com/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.