matlab中size()的用法

本文详细介绍了Matlab中size函数的使用方法,包括如何获取矩阵的行数和列数、如何将矩阵的尺寸分配给多个变量等。同时,还解释了在不同情况下size函数的行为差异。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

原文来自: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:

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)+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
### Matlab `size` 函数详解 #### 获取矩阵维度 `size` 函数用于返回数组各维度的大小。对于二维数组(即矩阵),此函数会给出行数和列数。 ```matlab A = [1 2; 3 4]; sz = size(A); disp(sz); % 显示结果为:2 2 ``` 当应用于多维数组时,`size` 返回的是每一维度上的长度向量[^1]。 #### 单独获取某维度尺寸 如果只需要知道特定维度的方向,则可以指定第二个参数来单独查询该方向上的元素数量: ```matlab B = rand(3, 4, 5); rows = size(B, 1); % 行数 columns = size(B, 2); % 列数 pages = size(B, 3); % 第三维度的数量 ``` 上述代码片段展示了如何分别获得三维随机数组 B 的各个维度大小。 #### 使用 `[m,n,...,p] = size(X)` 形式分配多个输出变量 为了更方便地处理高维数据结构,还可以通过定义多个左侧运算符的方式一次性提取所有维度的信息: ```matlab [m, n, p] = size(rand(7,8,9)); fprintf('The dimensions are %d by %d by %d.\n', m, n, p); % 输出:"The dimensions are 7 by 8 by 9." ``` 这里说明了怎样利用逗号分隔列表的形式将不同维度的结果赋给不同的变量。 #### 特殊情况下的行为 对于空数组或标量输入,`size` 函数也有相应的规定: - 对于任何形状为空的情况(如 [] 或者 zeros(0,0)),它总是返回全零的向量; - 如果传入单个数值作为输入,则视为一维矢量,其唯一的非单位维度等于这个值本身。 这些特性使得 `size` 成为一种非常灵活且强大的工具,在编程过程中经常被用来动态调整算法逻辑以适应不同类型的数据集。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值