Why does sum([]) equal to zero?

1 次查看(过去 30 天)
Executing the following gives zero as the result.
sum([])
I expect an error or an empty matrix as the answer.

采纳的回答

MathWorks Support Team
A fundamental property of the SUM function is that if A and B are sets of numbers, then
sum(union(A, B)) = sum(A) + sum(B)
In order to maintain this property, a number must be returned for empty matrices. Thus the number 0 is returned for empty matrix inputs to the SUM function.
Note: the 'union' operation referred to above is a mathematical definition, different from the UNION function in MATLAB.

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Data Type Identification 的更多信息

产品


版本

R14SP2

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by