length - Length of largest array dimension - MATLAB (original) (raw)

Main Content

Length of largest array dimension

Syntax

Description

L = length([X](#budugvu-X)) returns the length of the largest array dimension in X. For vectors, the length is simply the number of elements. For arrays with more dimensions, the length is max(size(X)). The length of an empty array is zero.

example

Examples

collapse all

Find the length of a uniformly spaced vector in the interval [5,10].

Find the length of a 3-by-7 matrix of zeros.

X = zeros(3,7); L = length(X)

Create a string array and compute its length, which is the number of elements in each row.

X = ["a" "b" "c"; "d" "e" "f"]

X = 2×3 string "a" "b" "c" "d" "e" "f"

Create a structure with fields for Day and Month. Use the structfun function to apply length to each field.

S = struct('Day',[1 13 14 26],'Month',{{'Jan','Feb', 'Mar'}})

S = struct with fields: Day: [1 13 14 26] Month: {'Jan' 'Feb' 'Mar'}

L = structfun(@(field) length(field),S)

Input Arguments

collapse all

Input array, specified as a scalar, vector, matrix, or multidimensional array.

Complex Number Support: Yes

Tips

Extended Capabilities

expand all

Thelength function fully supports tall arrays. For more information, see Tall Arrays.

The length function fully supports GPU arrays. To run the function on a GPU, specify the input data as a gpuArray (Parallel Computing Toolbox). For more information, see Run MATLAB Functions on a GPU (Parallel Computing Toolbox).

Version History

Introduced before R2006a