sum
fi
数组元素的总和
说明
示例
向量元素的总和
创建一个 fi
向量,并在构造函数中指定 fimath
属性。
A = fi([1 2 5 8 5], 'SumMode', 'KeepLSB', 'SumWordLength', 32)
A = 1 2 5 8 5 DataTypeMode: Fixed-point: binary point scaling Signedness: Signed WordLength: 16 FractionLength: 11 RoundingMethod: Nearest OverflowAction: Saturate ProductMode: FullPrecision SumMode: KeepLSB SumWordLength: 32 CastBeforeSum: true
计算 A
的元素的总和。
S = sum(A)
S = 21 DataTypeMode: Fixed-point: binary point scaling Signedness: Signed WordLength: 32 FractionLength: 11 RoundingMethod: Nearest OverflowAction: Saturate ProductMode: FullPrecision SumMode: KeepLSB SumWordLength: 32 CastBeforeSum: true
输出 S
是标量,其 SumWordLength
指定为 32
。S
的 FractionLength
是 11
,因为 SumMode
设置为 KeepLSB
。
每列中元素的总和
创建一个 fi
数组,并计算每列中元素的总和。
A=fi([1 2 8;3 7 0;1 2 2])
A = 1 2 8 3 7 0 1 2 2 DataTypeMode: Fixed-point: binary point scaling Signedness: Signed WordLength: 16 FractionLength: 11
S=sum(A)
S = 5 11 10 DataTypeMode: Fixed-point: binary point scaling Signedness: Signed WordLength: 18 FractionLength: 11
MATLAB® 返回一个行向量,其中包含 A
中每列的总和。S
的 WordLength
增大了两位,因为 ceil(log2(size(A,1)))=2
。FractionLength
保持不变,因为 SumMode
的默认设置是 FullPrecision
。
每行中元素的总和
沿 3×3 矩阵 A
的第二个维度 (dim=2
) 计算总和。
A=fi([1 2 8;3 7 0;1 2 2])
A = 1 2 8 3 7 0 1 2 2 DataTypeMode: Fixed-point: binary point scaling Signedness: Signed WordLength: 16 FractionLength: 11
S=sum(A, 2)
S = 11 10 5 DataTypeMode: Fixed-point: binary point scaling Signedness: Signed WordLength: 18 FractionLength: 11
MATLAB® 返回一个由每行中元素的总和组成的列向量。S
的 WordLength
是 18,因为 ceil(log2(size(A,2)))=2
。
保留数据类型的前提下计算元素的总和
计算 A
的列的总和,并使输出数组 S
具有与之相同的数据类型。
A = fi([1 2 8;3 7 0;1 2 2])
A = 1 2 8 3 7 0 1 2 2 DataTypeMode: Fixed-point: binary point scaling Signedness: Signed WordLength: 16 FractionLength: 11
class(A)
ans = 'embedded.fi'
S = sum(A, 'native')
S = 5 11 10 DataTypeMode: Fixed-point: binary point scaling Signedness: Signed WordLength: 18 FractionLength: 11
class(S)
ans = 'embedded.fi'
MATLAB® 保留了 A
的数据类型,并返回 embedded.fi
类型的行向量 S
。
输入参数
A
— 输入 fi
数组
fi
对象 | 数值变量
fi
输入数组,指定为标量、向量、矩阵或多维数组。
数据类型: single
| double
| int8
| int16
| int32
| int64
| uint8
| uint16
| uint32
| uint64
| fi
复数支持: 是
dim
— 运算维度
正整数标量
运算维度,指定为正整数标量。dim
也可以是 fi
对象。如果未指定值,则默认值是大小不等于 1 的第一个数组维度。
数据类型: single
| double
| int8
| int16
| int32
| int64
| uint8
| uint16
| uint32
| uint64
| fi
type
— 输出类
'double'
| 'native'
输出类,指定为 'double'
或 'native'
。输出类定义执行运算和返回运算时所用的数据类型。
如果
type
是'double'
,则sum
返回双精度数组,而不考虑输入数据类型。如果
type
是'native'
,则sum
返回与输入数组A
具有相同类的数组。
数据类型: char
输出参数
S
— 总和数组
标量 | 向量 | 矩阵 | 多维数组
总和数组,以标量、向量、矩阵或多维数组形式返回。
注意
fimath
对象用于计算总和。如果将 SumMode
设置为 FullPrecision
、KeepLSB
或 KeepMSB
,则 sum(A)
的整数增长位数为 ceil(log2(size(A,dim)))
。
限制
sum
不支持数据类型为Boolean
的fi
对象。
扩展功能
C/C++ 代码生成
使用 MATLAB® Coder™ 生成 C 代码和 C++ 代码。
使用说明和限制:
仅当起控制作用的
fimath
对象的SumMode
属性设置为SpecifyPrecision
或KeepLSB
时,才支持可变大小的输入。
HDL 代码生成
使用 HDL Coder™ 为 FPGA 和 ASIC 设计生成 VHDL、Verilog 和 SystemVerilog 代码。
版本历史记录
在 R2006a 之前推出
MATLAB 命令
您点击的链接对应于以下 MATLAB 命令:
请在 MATLAB 命令行窗口中直接输入以执行命令。Web 浏览器不支持 MATLAB 命令。
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list:
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)