matlab—size用法

来源:互联网 发布:数据库百科 编辑:程序博客网 时间:2024/06/10 01:48

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 arrayX 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) returnsthesize of matrix X in separate variablesm and n.

m = size(X,dim) returnsthesize of the dimension of X specified by scalardim.

[d1,d2,d3,...,dn] = size(X), for n >1, returns the sizes of the dimensions of the arrayX inthe variables d1,d2,d3,...,dn, provided the number of output argumentsn equals ndims(X).If n does not equal ndims(X),the following exceptions hold:

n < ndims(X)

di equals the size of the ithdimension of X for , butdn equalsthe product of the sizes of the remaining dimensions of X, thatis, dimensionsn through ndims(X).

n > ndims(X)

size returns ones in the "extra"variables, that is, those corresponding tondims(X)+1 through 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 = size(rand(2,3,4),2)m =     3

Here the size is output as a single vector.

d = size(rand(2,3,4))d =     2     3     4

Here the size of each dimension is assigned to a separate variable.

[m,n,p] = size(rand(2,3,4))m =     2n =     3p =     4

Example 2

If X = ones(3,4,5), then

[d1,d2,d3] = size(X)d1 =       d2 =       d3 =     3          4          5

But when the number of output variables is less than ndims(X):

[d1,d2] = size(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] = size(X)d1 =       d2 =       d3 =     3          4          5d4 =       d5 =       d6 =     1          1          1

原创粉丝点击