Skip to content

length

Length of the longest dimension.

n = length(X)

Returns the largest dimension size of X. For a vector, this is the number of elements. For a matrix, it’s max(size(X)). For an empty array, returns 0.

For most array-shape questions, numel (total element count) or size (per-dimension) is more appropriate; length is most useful for vectors.

length([10 20 30 40]) % 4
length(zeros(3,5)) % 5
length([]) % 0
  • size — Sizes along all dimensions, or along one specific dimension.
  • numel — Total number of elements.
  • ndims — Number of dimensions of the input.
  • isempty — Test whether the input has zero elements.