length

Length of the largest dimension of an array.

Category: Language Fundamentals > Matrices and Arrays

Syntax

n = length(A)

Returns max(size(A)), or 0 for an empty array.

Description

For a vector, length is the number of elements, which is its main use in loops: for k = 1:length(v). For a matrix it is the larger of rows and columns, which is rarely what you want, so prefer size(A, 1), size(A, 2) or numel when the array is not a vector.

Any array with a zero dimension has length 0, even zeros(3, 0).

Input arguments

A
Array of any type.

Output arguments

n
Nonnegative integer scalar.

Example

length([4 5 6 7])
length(ones(3, 7))
length('hello')
length(zeros(3, 0))
Output
ans = 4
ans = 7
ans = 5
ans = 0

See also

size numel ndims isempty