sum函数

强烈置顶,sum函数不仅仅可以求取矩阵中各个列向量的和,也可以求取各个行向量的和,并不需要对矩阵进行转置的操作。

sum 

Sum of array elements

阵列元素的和

Syntax

语法

B = sum(A)

B = sum(A, dim)

B = sum(A, 'double')

B = sum(A, dim, 'double')

B = sum(A, 'native')

B = sum(A, dim, 'native')

B=sum(A,dim)

Description

描述

B = sum(A) returns sums along different dimensions of an array.

B=sum(A) 返回沿着一个阵列的不同维度求和。

If A is a vector, sum(A) returns the sum of the elements.

如果A是一个向量,sum(A)返回元素的和。

If A is a matrix, sum(A) treats the columns of A as vectors, returning a row vector of the sums of each column. 

如果A是一个矩阵,sum(A)对待A的行如同向量,返回一个每行的和的列向量。

If A is a multidimensional array, sum(A) treats the values along the first non-singleton dimension as vectors, returning an array of row vectors. 

如果A是一个多维阵列,sum(A)对待值沿着第一个非单精度维度作为向量,返回一个列向量值。

B = sum(A,dim) sums along the dimension of A specified by scalar dim.

B=sum(A,dim) sum沿着通过标定dim值的特定A的维度。dim的值可以是1或2,当dim的值为1时,B返回A矩阵的各个列向量的和,当dim的值为2时,B返回A矩阵的各个行向量的和。

B = sum(..., 'double') performs additions in double-precision and return an answer of type double, even if A has data type single or an integer data type. This is the default for integer data types.

B=sum(...,'double') 在双精度中执行添加并返回一个双精度类型的答案,甚至如果A有数据单精度类型或一个整数数据类型。这是定义为整数数据类型。

B = sum(..., 'native') performs additions in the native data type of A and return an answer of the same data type. This is the default for single and double.

B=sum(...,'native') A的本地数据类型中执行添加并返回一个相同数据类型的答案。这是定义为单精度和双精度。

Remarks

备注

sum(diag(X)) is the trace of X.

sum(diag(X))X的迹。

Examples

例如一:

The magic square of order 3 is

三阶魔幻矩阵是:

M = magic(3) 

M = 

     8    1    6

     3    5    7

     4    9    2

This is called a magic square because the sums of the elements in each column are the same.

这是被称为一个魔幻矩阵因为每行元素的和是想等的。

sum(M) =

     15    15    15

as are the sums of the elements in each row, obtained by transposing:

每列中元素的和,通过转置得到:

sum(M') = 

     15    15    15

Nondouble Data Type Support

支持非双精度数据类型

This section describes the support of sum for data types other than double.

这时sum支持排除双精度类型以外的数据类型的描述部分。

Data Type single

单精度数据类型

You can apply sum to an array of type single and MATLAB returns an answer of type single. For example,

你能够应用sum 到一个单精度数据类型的阵列中并且MATLAB返回一个单精度类型的答案。例如,

sum(single([2 5 8]})

ans =

     15

class(ans)

ans =

Single

Integer Data Types

整数数据类型

When you apply sum to any of the following integer data types, MATLAB returns an answer of type double:

当你应用sum 到任何以下整数数据类型时,MATLAB返回一个双精度类型的答案:

·int8 and uint8 

·int16 and uint16 

·int32 and uint32

For example,

例如,

sum(single([2 5 8]);

class(ans)

ans =

Single

If you want MATLAB to perform additions on an integer data type in the same integer type as the input, use the syntax

如果你想要MATLAB在相同的整数类型中一个整数数据类型上执行添加作为输入,使用语法

sum(int8([2 5 8], 'native');

class(ans)

ans =

int8

例如二:

A=[1 2 3 4

1 2 3 4]

A=

1 2 3 4

1 2 3 4

B=sum(A,1)

B=

2 4 6 8

B=sum(A,2)

B=

10

10

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值