squeeze

Removes dimensions of length 1 from an array.

Category: Language Fundamentals > Matrices and Arrays

Syntax

B = squeeze(A)

Returns A without its singleton dimensions.

Description

A slice such as A(1,:,:) keeps a leading dimension of length 1. squeeze discards every size-1 dimension so the slice becomes an ordinary 2-D matrix. The elements and their order are not changed, only the size vector.

A 2-D array is returned unchanged, and the result always has at least two dimensions: a 1x1x5 array becomes 5x1, a column.

Input arguments

A
Array of any type and size.

Output arguments

B
Same data as A with the size-1 dimensions dropped.

Example

A = reshape(1:6, 1, 2, 3);
size(A)
B = squeeze(A)
size(squeeze(ones(1,1,4)))
Output
ans = 1×3

     1     2     3
B = 2×3

     1     3     5
     2     4     6
ans = 1×2

     4     1

See also

reshape permute size cat