matlab—size用法

MATLAB的size函数用于获取矩阵的尺寸。对于不同输入,它返回矩阵的行数、列数或多维尺寸。例如,size(A)返回一个包含矩阵大小的行向量,size(A,1)返回行数,size(A,2)返回列数。当A是n维行向量时,size(A)返回1×n。示例包括二维和多维矩阵的尺寸查询。" 104265397,8656014,解决阿里云CentOS安装MySQL依赖问题,"['Linux', 'MySQL', '系统管理', '依赖解决', 'CentOS']

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:

n < ndims(X)

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

n > ndims(X)

size returns ones in the "extra"variables, that is, those corresponding to ndims(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 =
     2

n =
     3

p =
     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          5

d4 =       d5 =       d6 =
     1          1          1

### MATLAB `size` 函数使用方法 #### 语法说明 `size` 函数用于返回数组各个维度的大小。对于不同类型的输入,`size` 的行为有所不同。 - 对于二维矩阵 A, `d = size(A)` 返回包含两个元素的行向量 d,其中第一个元素是行数,第二个元素是列数[^4]。 ```matlab A = [1 2; 3 4]; sz = size(A); disp(sz); % 显示:2 2 ``` - 如果 a 是 n 维行向量,则 `size(a)` 把它当作一个 \(1 \times n\) 的矩阵处理,因此 `size(a)` 的结果将是 `[1 n]`。 ```matlab v = [1 2 3 4 5]; sz_v = size(v); disp(sz_v); % 显示:1 5 ``` - 若要获取特定维度的长度,可指定第二参数 dim 来获得第 dim 维度上的尺寸: ```matlab matrix_3D = rand(4, 5, 6); rows = size(matrix_3D, 1); % 获取第一维(即行)的数量 columns = size(matrix_3D, 2); % 获取第二维(即列)的数量 pages = size(matrix_3D, 3); % 获取第三维的数量 ``` - 当需要将多个维度合并成单个输出变量时,可以通过设置某维度为单独的输出来实现这一点。例如,在三维或多维情况下,如果只想知道除了某一固定维度外其他所有维度乘积后的总数目,那么可以让那个固定的维度作为最后一个被计算出来的输出值: ```matlab [m, n, p] = size(rand(7,8,9)); % 或者更通用的方式: [sz1, szRest...] = size(CustomArray); ``` #### 示例代码 这里给出几个具体的例子来展示 `size` 函数的应用场景: ```matlab % 创建一个多维数组并查看其各维度大小 multiDimArr = ones(2, 3, 4); % 调用 size() 查看整个多维数组的信息 totalSize = size(multiDimArr); fprintf('The total sizes are: [%d,%d,%d]\n', ... totalTime{1}, totalTime{2}, totalTime{3}); % 只查询某个具体维度的大小 dimTwoSize = size(multiDimArr, 2); fprintf('Dimension two has size of %d\n', dimTwoSize); ```
评论 2
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值