squeeze — Remove singleton dimensions while preserving MATLAB-compatible vector orientation semantics.
squeeze(A) removes dimensions of size one from A while preserving MATLAB-compatible row/column vector orientation rules. Higher-rank arrays collapse only where singleton dimensions exist.
Syntax
B = squeeze(A)Inputs
A- Type
Any- Required
- Yes
- Description
- Input array/value.
Returns
B- Type
Any- Description
- Input value with singleton dimensions removed (2-D minimum preserved).
Errors
RunMat:squeeze:InvalidInput- When
- A has an unsupported type for squeeze.
- Message
squeeze: unsupported input type
Behavior
Dimensions greater than two drop all size-one axes. If exactly one dimension remains, the output becomes an N × 1 column vector.
Two-dimensional inputs (row/column vectors and matrices) are returned unchanged.
Zero-length dimensions are preserved; only length-one axes are removed.
Scalar values remain scalars.
Numeric storage is only reshaped: single, double, and all eight integer classes preserve their native class and exact payload, including wide integer values.
GPU outputs stay on the owning device. Contiguous shape views retain the allocation; lazy device layouts materialize through their exact owner when shape changes.
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
Removing Singleton Dimensions from a 3-D Matrix
A = reshape(1:12, [1, 3, 4]);
B = squeeze(A);
size(B)Expected output:
ans = [3 4]Turning 1x1xN Slices into Column Vectors
T = zeros(1, 1, 5);
T(:, :, 3) = 7;
vec = squeeze(T);
size(vec)Expected output:
ans = [5 1]Keeping Row Vectors Unchanged After Squeeze
row = rand(1, 8);
out = squeeze(row);
size(out)Expected output:
ans = [1 8]Squeezing Logical Masks from Simulations
mask = false(1, 10, 1, 1);
mask(1, 4, 1, 1) = true;
flat = squeeze(mask)Expected output:
flat = [0 0 0 1 0 0 0 0 0 0]Using Squeeze on GPU-Resident Data
G = gpuArray(rand(1, 64, 1));
H = squeeze(G);
isgpuarray(H)Expected output:
ans = logical 1Squeezing String Arrays While Preserving Layout
S = strings(1, 1, 3);
S(1, 1, 1) = "run";
S(1, 1, 2) = "mat";
S(1, 1, 3) = "gpu";
T = squeeze(S);
size(T)Expected output:
ans = [3 1]Using squeeze with coding agents
Open a RunMat example with live inputs, then ask the agent to explain how squeeze changes the result.
Run a small squeeze example, explain the result, then change one input and compare the output.
FAQ
Why does squeeze keep 2-D matrices unchanged?⌄
MATLAB treats row and column vectors as 2-D objects. Preserving those dimensions avoids turning row vectors into column vectors or scalars unintentionally.
What happens when every dimension is singleton?⌄
The result becomes a 1 × 1 array. Scalars (e.g., plain doubles) remain scalars.
Does squeeze affect zero-length dimensions?⌄
No. Only size-one dimensions are removed. Zero-length dimensions are preserved exactly.
Can I squeeze character or cell arrays?⌄
Yes. Character and cell arrays use their stored shape; higher-dimensional singleton axes are removed and the two-dimensional minimum is retained.
How does squeeze interact with GPU tensors?⌄
Contiguous device inputs use allocation-retaining shape views. A changed lazy device layout is materialized through its exact provider before publication; there is no speculative metadata gather.
What if I need to remove a specific dimension?⌄
Use reshape, permute, or indexing to reorganize axes manually before calling squeeze.
Does squeeze change data ordering?⌄
Logical linear element order is preserved. Contiguous device views can share their allocation; changed lazy layouts may materialize a different physical backing.
Will squeeze ever increase the number of dimensions?⌄
No. It either returns the same number or fewer. The only exception is padding with a trailing singleton to maintain MATLAB's column-vector convention when exactly one dimension remains.
Is squeeze safe to run before GPU fusion?⌄
Yes. Later eligible operations can fuse using the resulting shape. The squeeze call is a structural fusion boundary and may materialize a lazy device layout; it is not part of an elementwise shader chain.
How do I undo a squeeze?⌄
Use reshape with the original size vector (for example, captured via size(A)) to restore the previous dimension layout.
Related Array functions
Shape
blkdiag · cat · circshift · diag · flip · fliplr · flipud · horzcat · ipermute · kron · permute · repelem · repmat · reshape · rot90 · shiftdim · 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 squeeze is executed, line by line, in Rust.
- View the source for squeeze 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.