主要内容

size

描述

example

sz=大小A)返回一个行矢量,其元素是相应维度的长度A。例如,如果Ais a 3-by-4 matrix, then尺寸(a)returns the vector[3 4]

如果Ais a table or timetable, then尺寸(a)返回由行数和表变量数组成的两元素行向量。

example

szdim=大小A,dim)returns the length of dimensiondimwhendim是一个积极的整数标量。从R2019B开始,您也可以指定dim作为正整数的向量,一次查询多维长度。例如,size(A,[2 3])returns the lengths of the second and third dimensions ofAin the 1-by-2 row vectorszdim

example

szdim=大小A,dim1,dim2,…,dimN)返回尺寸的长度dim1,dim2,…,dimNin the row vectorszdim(starting in R2019b).

example

[SZ1,...,SZN] = size(___)returns the lengths of the queried dimensions ofAseparately.

例子

全部收缩

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

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

Query only the length of the second dimension ofA

szdim2 = size(a,2)
szdim2 = 3

从R2019B开始,您可以一次通过指定向量维度参数来查询多个维度长度。例如,找到第一和第三维的长度A

szdim13 = size(a,[1 3])
szdim13 =1×22 4

Find the lengths of the second through fourth dimensions ofA

szdim23 = size(a,2:4)
szdim23 =1×33 4 5

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

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

创建一个具有5行和4个变量的表。

lastName = {“史密斯”;'Johnson';'Williams';'Jones';'Brown'};年龄= [38; 43; 38; 40; 49];高度= [71; 69; 64; 67; 64];重量= [176; 163; 131; 133; 119];血压= [124 93;109 77;125 83;117 75;122 80];a =表(年龄,身高,体重,血压,'RowNames',LastName)
A=5×4桌年龄高度体重血压___ ______ ______ _____________史密斯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 64 119 122 80

找到表的大小。虽然血压variable contains two columns,sizeonly counts the number of variables.

sz = size(a)
sz =1×25 4

创建一个随机矩阵,然后分别返回行的数量和列的数量。

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

Input Arguments

全部收缩

输入数组, 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|细胞|分类|约会时间|duration|calendarDuration|桌子|timetable

Complex Number Support:Yes

查询尺寸,指定为正整数标量的正整数标量或向量。如果是dim大于ndims(A), thensizereturns1在输出的相应元素中。

Data Types:single|double|int8|INT16|INT32|int64|uint8|UINT16|uint32|uint64

查询尺寸列表, specified as positive integer scalars separated by commas. If an element of the list is larger thanndims(A), thensizereturns1在输出的相应元素中。

Data Types:single|double|int8|INT16|INT32|int64|uint8|UINT16|uint32|uint64

Output Arguments

全部收缩

数组大小,作为非负整数的行矢量返回。

  • Each element ofsz表示相应维度的长度A。如果any element ofszis equal to0, thenA是一个空数组。

  • 如果A是标量sz是行矢量[1 1]

  • 如果Ais a table or timetable, thenszis a two-element row vector containing the number of rows and the number of variables. Multiple columns within a single variable are not counted.

  • 如果Ais a character vector of typechar, thensizereturns the row vector[1 M]whereMis the number of characters. However, ifA是字符串标量,sizereturns[1 1]because it is a single element of a string array. For example, compare the output ofsizefor a character vector and string:

    szchar = size(大小'mytext')
    szchar = 1 6
    szstr = size("mytext")
    SZSTR = 1 1
    要查找字符串中的字符数,请使用strlengthfunction.

Data Types:double

Dimension lengths, returned as a nonnegative integer scalar whendim是一个标量和a row vector of nonnegative integer scalars whendimis a vector. If an element of the specified dimension argument is larger thanndims(A), thensizereturns1在相应的元素中szdim

Data Types:double

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

  • 什么时候dimis 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是一个尺寸的3-D阵列[3 4 5], then[sz1,sz2] = size(A)returnssz1 = 3sz2 = 20

  • 什么时候dim已指定,输出参数的数量必须等于查询尺寸的数量。

  • 如果您指定超过ndims(A)output arguments, then the extra trailing arguments are returned as1

Data Types:double

尖端

Extended Capabilities

C/C ++代码生成
使用MATLAB®CODER™生成C和C ++代码。

GPU Code Generation
使用GPU CODER™为NVIDIA®GPU生成CUDA®代码。

HDL Code Generation
使用HDL Coder™生成用于FPGA和ASIC设计的Verilog和VHDL代码。

在R2006a之前引入