ndims

Number of dimensions of an array.

Category: Language Fundamentals > Matrices and Arrays

Syntax

n = ndims(A)

Returns numel(size(A)).

Description

Every MATLAB array is at least two-dimensional, so scalars, vectors and matrices all give 2. Trailing dimensions of size 1 are dropped, so ones(2, 3, 1, 1) has two dimensions. A dimension of size 0 still counts.

Input arguments

A
Array of any type.

Output arguments

n
Integer, never less than 2.

Example

ndims(5)
ndims(ones(2, 3))
ndims(ones(2, 3, 4))
ndims(zeros(2, 0, 3))
Output
ans = 2
ans = 2
ans = 3
ans = 3

See also

size numel length squeeze