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.
[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
.
[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
.
[[sz1,...,szN](#bvfgzsm-1-sz1szN)] = size(___)
returns the lengths of the queried dimensions of A
separately.
Examples
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
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
Array size, returned as a row vector of nonnegative integers.
- Each element of
sz
represents the length of the corresponding dimension ofA
. If any element ofsz
is equal to0
, thenA
is an empty array. - If
A
is a scalar, thensz
is the row vector[1 1]
. - If
A
is a table or timetable, thensz
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 typechar
, thensize
returns the row vector[1 M]
whereM
is the number of characters. However, ifA
is a string scalar,size
returns[1 1]
because it is a single element of a string array. For example, compare the output ofsize
for a character vector and string:
To find the number of characters in a string, use the strlength function.
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.
- When
dim
is not specified and fewer thanndims(A)
output arguments are listed, then all remaining dimension lengths are collapsed into the last argument in the list. For example, ifA
is a 3-D array with size[3 4 5]
, then[sz1,sz2] = size(A)
returnssz1 = 3
andsz2 = 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 as1
.
Data Types: double
Tips
- To determine if an array is empty, a scalar, or a matrix, use the functionsisempty, isscalar, and ismatrix. You can also determine the orientation of a vector with the isrow and iscolumn functions.
Extended Capabilities
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
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.