User`s guide

Reduction Variables
2-37
f(e,a) = a = f(a,e)
Examples of identity elements for some functions are listed in this table.
Function Identity Element
+ 0
* and .* 1
[,] and [;] []
& true
| false
MATLAB uses the identity elements of reduction functions when it knows them. So, in
addition to associativity and commutativity, you should also keep identity elements in
mind when overloading these functions.
Recommended: An overload of +, *, .*, [,], or [;] should be associative if it is used
in a reduction assignment in a parfor. The overload must treat the respective identity
element given above (all with class double) as an identity element.
Recommended: An overload of +, .*, union, or intersect should be commutative.
There is no way to specify the identity element for a function. In these cases, the behavior
of parfor is a little less efficient than it is for functions with a known identity element,
but the results are correct.
Similarly, because of the special treatment of X = X - expr, the following is
recommended.
Recommended: An overload of the minus operator (-) should obey the mathematical
law that X - (y + z) is equivalent to (X - y) - z.
Example: Using a Custom Reduction Function
Suppose each iteration of a loop performs some calculation, and you are interested
in finding which iteration of a loop produces the maximum value. This is a reduction
exercise that makes an accumulation across multiple iterations of a loop. Your reduction
function must compare iteration results, until finally the maximum value can be
determined after all iterations are compared.