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
| Name | Type | Required | Default | Description |
|---|---|---|---|---|
A | Any | Yes | — | Input array/value. |
Returns
| Name | Type | Description |
|---|---|---|
B | Any | Input value with singleton dimensions removed (2-D minimum preserved). |
Errors
| Identifier | When | Message |
|---|---|---|
RunMat:squeeze:InvalidInput | A has an unsupported type for squeeze. | squeeze: unsupported input type |
How squeeze works
- Dimensions greater than two drop all size-one axes. If exactly one dimension remains, the output becomes an
N × 1column 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.
- GPU arrays stay resident on the device where possible; the runtime only gathers when it cannot infer the shape.
Does RunMat run squeeze on the GPU?
When a GPU provider is active, RunMat calls the provider's reshape hook to update tensor metadata without copying data. Providers that do not override reshape fall back to the in-process behaviour, simply updating the handle shape locally. If a handle advertises no shape, RunMat downloads metadata once to infer dimensions before applying squeeze.
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. They already store up to two dimensions in RunMat, so squeeze leaves them unchanged but still validates inputs.
How does squeeze interact with GPU tensors?⌄
The runtime updates GPU tensor metadata via the provider's reshape hook. Data stays on the device unless a provider cannot report shape information, in which case RunMat gathers once to infer dimensions.
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?⌄
No. It only alters metadata. Element order in memory remains unchanged.
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. Since it only updates shapes, it integrates cleanly with fusion and does not impede kernel generation.
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
Open-source implementation
Unlike proprietary runtimes, every RunMat function is open-source. 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 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.