size

Size of an array along each dimension.

Category: Language Fundamentals > Matrices and Arrays

Syntax

sz = size(A)

Row vector with the length of every dimension.

n = size(A, dim)

Length along dimension dim.

[r, c, ...] = size(A)

One output per dimension; the last absorbs any remaining dimensions.

sz = size(A, [d1 d2])

Lengths along several chosen dimensions.

Description

A scalar is [1 1], the empty [] is [0 0], and a character vector 'hello' is [1 5]. Asking for a dimension past ndims(A) returns 1.

When there are fewer outputs than dimensions, the last output holds the product of the rest: [r, c] = size(ones(2, 3, 4)) gives r = 2 and c = 12. Use size(A, 1) for rows and size(A, 2) for columns when you need only one number.

Input arguments

A
Array of any type.
dim
Dimension number, or vector of dimension numbers.

Output arguments

sz
Row vector of sizes, at least two elements.
r, c, ...
Sizes as separate scalars.

Example

A = ones(2, 3, 4);
size(A)
size(A, 2)
[r, c] = size(A)
Output
ans = 1×3

     2     3     4
ans = 3
r = 2
c = 12

See also

numel length ndims reshape isempty