转自:https://i-blog.csdnimg.cn/blog_migrate/53bf4dcf6ca711a4b7d0406eba27aecd.png
size(A)函数是用来求矩阵的大小的,你必须首先弄清楚A到底是什么,大小是多少。
比如说一个A是一个3×4的二维矩阵:
1、size(A) %直接显示出A大小
输出:ans=
3 4
2、s=size(A)%返回一个行向量s,s的第一个元素是矩阵的行数,第二个元素是矩阵的列数
输出:s=
3 4
3、[r,c]=size(A)%将矩阵A的行数返回到第一个输出变量r,将矩阵的列数返回到第二个输出变量c
输出:r=
3
c=
4
4、[r,c,m]=size(A)
输出:r=
3
c=
4
m=
1
也就说它把二维矩阵当作第三维为1的三维矩阵,这也如同我们把n维列向量当作n×1的矩阵一样
5、当a是一个n维行向量时,size(A)把其当成一个1×n的矩阵,因此size(a)的结果是
ans
1 n
而不是a的元素个数n
6、size(A,n)
如果在size函数的输入参数中再添加一项n,并用1或2为n赋值,则 size将返回矩阵的行数或列数。其中r=size(A,1)该语句返回的是矩阵A的行数, c=size(A,2) 该语句返回的是矩阵A的列数。
matlab中的解释如下:
Syntax
d = size(X)
[m,n] = size(X)
m = size(X,dim)
[d1,d2,d3,...,dn] = size(X),
Description
d = size(X) returns thesizes of each dimension of array X in a vector d with ndims(X) elements.If X is a scalar, which MATLAB software regardsas a 1-by-1 array, size(X) returns the vector [11].
[m,n] = size(X) returnsthe size of matrix X in separate variables m and n.
m = size(X,dim) returnsthe size of the dimension of X specified by scalar dim.
[d1,d2,d3,...,dn] = size(X), for n >1, returns the sizes of the dimensions of the array X inthe variables d1,d2,d3,...,dn, provided the number of output arguments n equals ndims(X).If n does not equal ndims(X),the following exceptions hold:
di equals the size of the ithdimension of X for | |
size returns ones in the "extra"variables, that is, those corresponding to ndims(X)+1through n. |
-
Note For a Java array, size returns the lengthof the Java array as the number of rows. The number of columns isalways 1. For a Java array of arrays, the result describes only thetop level array.
Examples
Example 1
The size of the second dimension of rand(2,3,4) is3.
m = <span class="highlight0">size</span>(rand(2,3,4),2) m = 3
Here the size is output as a single vector.
d = <span class="highlight0">size</span>(rand(2,3,4)) d = 2 3 4
Here the size of each dimension is assigned to a separate variable.
[m,n,p] = <span class="highlight0">size</span>(rand(2,3,4)) m = 2 n = 3 p = 4
Example 2
If X = ones(3,4,5), then
[d1,d2,d3] = <span class="highlight0">size</span>(X) d1 = d2 = d3 = 3 4 5
But when the number of output variables is less than ndims(X):
[d1,d2] = <span class="highlight0">size</span>(X) d1 = d2 = 3 20
The "extra" dimensions are collapsed into a singleproduct.
If n > ndims(X), the "extra"variables all represent singleton dimensions:
[d1,d2,d3,d4,d5,d6] = <span class="highlight0">size</span>(X) d1 = d2 = d3 = 3 4 5 d4 = d5 = d6 = 1 1 1