size - Array size - MATLAB (original) (raw)

Syntax

Description

[sz](#bvfgzsm-1-sz) = size([A](#bvfgzsm-1-A)) returns a row vector whose elements are the lengths of the corresponding dimensions of A. For example, if A is a 3-by-4 matrix, then size(A) returns the vector [3 4].

If A is a table or timetable, then size(A) returns a two-element row vector consisting of the number of rows and the number of table variables.

example

[szdim](#bvfgzsm-1-szdim) = size([A](#bvfgzsm-1-A),[dim](#bvfgzsm-1-dim)) returns the length of dimension dim when dim is a positive integer scalar. You can also specify dim as a vector of positive integers to query multiple dimension lengths at a time. For example, size(A,[2 3]) returns the lengths of the second and third dimensions of A in the 1-by-2 row vectorszdim.

example

[szdim](#bvfgzsm-1-szdim) = size([A](#bvfgzsm-1-A),[dim1,dim2,...,dimN](#mw%5Fdda7c7f5-9ad9-4379-a52f-253516c02f19)) returns the lengths of dimensions dim1,dim2,...,dimN in the row vector szdim.

example

[[sz1,...,szN](#bvfgzsm-1-sz1szN)] = size(___) returns the lengths of the queried dimensions of A separately.

example

Examples

collapse all

Create a random 4-D array and return its size.

A = rand(2,3,4,5); sz = size(A)

Query the length of the second dimension of A.

Query the length of the last dimension of A.

szdimlast = size(A,ndims(A))

You can query multiple dimension lengths at a time by specifying a vector dimension argument. For example, find the lengths of the first and third dimensions of A.

Find the lengths of the second through fourth dimensions of A.

Alternatively, you can list the queried dimensions as separate input arguments.

Create a table with 5 rows and 4 variables.

LastName = {'Smith';'Johnson';'Williams';'Jones';'Brown'}; Age = [38;43;38;40;49]; Height = [71;69;64;67;64]; Weight = [176;163;131;133;119]; BloodPressure = [124 93; 109 77; 125 83; 117 75; 122 80];

A = table(Age,Height,Weight,BloodPressure,'RowNames',LastName)

A=5×4 table Age Height Weight BloodPressure ___ ______ ______ _____________

Smith       38       71       176       124     93  
Johnson     43       69       163       109     77  
Williams    38       64       131       125     83  
Jones       40       67       133       117     75  
Brown       49       64       119       122     80  

Find the size of the table. Although the BloodPressure variable contains two columns, size only counts the number of variables.

Create a random matrix and return the number of rows and columns separately.

A = rand(4,3); [numRows,numCols] = size(A)

Input Arguments

collapse all

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

Data Types: single | double |int8 | int16 |int32 | int64 |uint8 | uint16 |uint32 | uint64 |logical | char |string | struct |function_handle | cell |categorical | datetime |duration | calendarDuration |table | timetable

Complex Number Support: Yes

Queried dimensions, specified as a positive integer scalar, a vector of positive integer scalars, or an empty array of size 0-by-0, 0-by-1, or 1-by-0. If an element of dim is larger thanndims(A), then size returns1 in the corresponding element of the output. Ifdim is an empty array, then size returns a 1-by-0 empty array.

Data Types: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64

List of queried dimensions, specified as positive integer scalars separated by commas. If an element of the list is larger thanndims(A), then size returns1 in the corresponding element of the output.

Data Types: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64

Output Arguments

collapse all

Array size, returned as a row vector of nonnegative integers.

Data Types: double

Dimension lengths, returned as a nonnegative integer scalar whendim is a positive integer scalar, a row vector of nonnegative integer scalars when dim is a vector of positive integers, or a 1-by-0 empty array when dim is an empty array. If an element of the specified dimension argument is larger than ndims(A), then size returns1 in the corresponding element ofszdim.

Data Types: double

Dimension lengths listed separately, returned as nonnegative integer scalars separated by commas.

Data Types: double

Tips

Extended Capabilities

expand all

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

The size 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

expand all

You can specify dim as a vector of positive integers to query multiple dimension lengths at a time. Alternatively, you can list the queried dimensions as separate input arguments dim1,dim2,...,dimN. For an example, see Size of 4-D Array.