size
Array size
Description
returns a row vector whose elements are the lengths of the corresponding dimensions
of sz
= size(A
)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.
returns the length of dimension szdim
= size(A
,dim
)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 vector
szdim
.
returns the lengths of dimensions szdim
= size(A
,dim1,dim2,...,dimN
)dim1,dim2,...,dimN
in the row
vector szdim
.
[
returns the lengths of the queried dimensions of sz1,...,szN
] = size(___)A
separately.
Examples
Size of 4-D Array
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 the length of the second dimension of A
.
szdim2 = size(A,2)
szdim2 = 3
Query the length of the last dimension of A
.
szdimlast = size(A,ndims(A))
szdimlast = 5
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);
Size of Table
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
Dimension Lengths as Separate Arguments
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
A
— Input array
scalar | vector | matrix | multidimensional array
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
dim
— Queried dimensions
positive integer scalar | vector of positive integer scalars | empty array
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 than
ndims(A)
, then size
returns
1
in the corresponding element of the output. If
dim
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
dim1,dim2,...,dimN
— List of queried dimensions
positive integer scalars
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
sz
— Array size
row vector of nonnegative integers
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:szchar = size('mytext')
szchar = 1 6
szstr = size("mytext")
To find the number of characters in a string, use theszstr = 1 1
strlength
function.
Data Types: double
szdim
— Dimension lengths
nonnegative integer scalar | vector of nonnegative integer scalars | 1-by-0 empty array
Dimension lengths, returned as a nonnegative integer scalar when
dim
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
returns
1
in the corresponding element of
szdim
.
Data Types: double
sz1,...,szN
— Dimension lengths listed separately
nonnegative integer scalars
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
Extended Capabilities
Tall Arrays
Calculate with arrays that have more rows than fit in memory.
The
size
function fully supports tall arrays. For more information,
see Tall Arrays.
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 VHDL, Verilog and SystemVerilog code for FPGA and ASIC designs using HDL Coder™.
Thread-Based Environment
Run code in the background using MATLAB® backgroundPool
or accelerate code with Parallel Computing Toolbox™ ThreadPool
.
This function fully supports thread-based environments. For more information, see Run MATLAB Functions in Thread-Based Environment.
GPU Arrays
Accelerate code by running on a graphics processing unit (GPU) using Parallel Computing Toolbox™.
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).
Distributed Arrays
Partition large arrays across the combined memory of your cluster using Parallel Computing Toolbox™.
This function fully supports distributed arrays. For more information, see Run MATLAB Functions with Distributed Arrays (Parallel Computing Toolbox).
Version History
Introduced before R2006aR2019b: Specify dimensions as vector of positive integers or separate input arguments
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.
MATLAB Command
You clicked a link that corresponds to this MATLAB command:
Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list:
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)