squeeze — Remove singleton (size-one) dimensions while preserving MATLAB vector semantics.
squeeze(A) removes singleton (dimension size equal to one) dimensions from A. The result keeps MATLAB's row/column vector semantics—2-D matrices stay 2-D, and higher-rank arrays collapse down until only meaningful axes remain.
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.
How RunMat runs 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]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 works, line by line, in Rust.
- View squeeze.rs on GitHub
- Learn how the 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 — faster, on any GPU, with no license required.
- Simulations that took hours now take minutes. RunMat automatically optimizes your math for GPU execution on Apple, Nvidia, and AMD hardware. No code changes needed.
- Start running code in seconds. Open the browser sandbox or download a single binary. No license server, no IT ticket, no setup.
- A full development environment. GPU-accelerated 2D and 3D plotting, automatic versioning on every save, and a browser IDE you can share with a link.