Main Content

size

Description

example

sz = size(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 = size(A,dim) returns the length of dimension dim when dim is a positive integer scalar. Starting in R2019b, 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 vector szdim.

example

szdim = size(A,dim1,dim2,…,dimN) returns the lengths of dimensions dim1,dim2,…,dimN in the row vector szdim (starting in R2019b).

example

[sz1,...,szN] = size(___) returns the lengths of the queried dimensions of A separately.

Examples

collapse all

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

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

     2     3     4     5

Query only the length of the second dimension of A.

szdim2 = size(A,2)
szdim2 = 3

Starting in R2019b, 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.

szdim13 = size(A,[1 3])
szdim13 = 1×2

     2     4

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

szdim23 = size(A,2:4)
szdim23 = 1×3

     3     4     5

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

szdim23 = size(A,2,3,4);

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.

sz = size(A)
sz = 1×2

     5     4

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

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

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 or vector of positive integer scalars. If an element of dim is larger than ndims(A), then size returns 1 in the corresponding element of the output.

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 than ndims(A), then size returns 1 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.

  • Each element of sz represents the length of the corresponding dimension of A. If any element of sz is equal to 0, then A is an empty array.

  • If A is a scalar, then sz is the row vector [1 1].

  • If A is a table or timetable, then sz is a two-element row vector containing the number of rows and the number of variables. Multiple columns within a single variable are not counted.

  • If A is a character vector of type char, then size returns the row vector [1 M] where M is the number of characters. However, if A is a string scalar, size returns [1 1] because it is a single element of a string array. For example, compare the output of size for a character vector and string:

    szchar = size('mytext')
    szchar =
    
         1     6
    szstr = size("mytext")
    szstr =
    
         1     1
    
    To find the number of characters in a string, use the strlength function.

Data Types: double

Dimension lengths, returned as a nonnegative integer scalar when dim is a scalar and a row vector of nonnegative integer scalars when dim is a vector. If an element of the specified dimension argument is larger than ndims(A), then size returns 1 in the corresponding element of szdim.

Data Types: double

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

  • When dim is not specified and fewer than ndims(A) output arguments are listed, then all remaining dimension lengths are collapsed into the last argument in the list. For example, if A is a 3-D array with size [3 4 5], then [sz1,sz2] = size(A) returns sz1 = 3 and sz2 = 20.

  • When dim is specified, the number of output arguments must equal the number of queried dimensions.

  • If you specify more than ndims(A) output arguments, then the extra trailing arguments are returned as 1.

Data Types: double

Tips

  • To determine if an array is empty, a scalar, or a matrix, use the functions isempty, isscalar, and ismatrix. You can also determine the orientation of a vector with the isrow and iscolumn functions.

Extended Capabilities

C/C++ Code Generation
Generate C and C++ code using MATLAB® Coder™.

GPU Code Generation
Generate CUDA® code for NVIDIA® GPUs using GPU Coder™.

HDL Code Generation
Generate Verilog and VHDL code for FPGA and ASIC designs using HDL Coder™.

Introduced before R2006a