- 格式为sum(A)
>> a=1:10;
>> sum(a)
ans =
55
>>%矩阵时,生成的是矩阵的列和
>> a=[1 2 3
4 5 6
7 8 9];
>> sum(a)
ans =
12 15 18
>>
- 格式为sum(A,dim)
%当dim=1时,求得是矩阵的列和,dim=2时求得是行和
a = [1 2 3
4 5 6
7 8 9];
>> sum(a,1)
ans =
12 15 18
>> sum(a,2)
ans =
6
15
24
- 格式为sum(a,type),type主要是控制sum的数据的种类
“`
a = [1 2 3
4 5 6
7 8 9];int8(a)
ans =
1 2 3
4 5 6
7 8 9
b = sum(a)
b =
12 15 18
whos
Name Size Bytes Class Attributes
A 3x3 72 double
a 3x3 72 double
ans 3x3 9 int8
b 1x3 24 double
“`