If you are working with the integer classes, and you mix a NaN into arithmetic calculations with the data, then the result differs depending on whether the NaN appears in the first operation or if the NaN is encountered afterwards. At least on R2014a that is.
X = {NaN, uint8(2), uint8(3), uint8(4)};T = perms(X);total = @(A,B,C,D) A+B+C+D;for K = 1 : size(T,1); result{K} = total(T{K,1},T{K,2},T{K,3},T{K,4}); end[result(:),T]
ans =
[0] [ 4] [ 3] [ 2] [NaN] [2] [ 4] [ 3] [NaN] [ 2] [0] [ 4] [ 2] [ 3] [NaN] [3] [ 4] [ 2] [NaN] [ 3] [5] [ 4] [NaN] [ 2] [ 3] [5] [ 4] [NaN] [ 3] [ 2] [0] [ 3] [ 4] [ 2] [NaN] [2] [ 3] [ 4] [NaN] [ 2] [0] [ 3] [ 2] [ 4] [NaN] [4] [ 3] [ 2] [NaN] [ 4] [6] [ 3] [NaN] [ 2] [ 4] [6] [ 3] [NaN] [ 4] [ 2] [0] [ 2] [ 3] [ 4] [NaN] [4] [ 2] [ 3] [NaN] [ 4] [0] [ 2] [ 4] [ 3] [NaN] [3] [ 2] [ 4] [NaN] [ 3] [7] [ 2] [NaN] [ 4] [ 3] [7] [ 2] [NaN] [ 3] [ 4] [6] [NaN] [ 3] [ 2] [ 4] [6] [NaN] [ 3] [ 4] [ 2] [7] [NaN] [ 2] [ 3] [ 4] [7] [NaN] [ 2] [ 4] [ 3] [5] [NaN] [ 4] [ 2] [ 3] [5] [NaN] [ 4] [ 3] [ 2]
It took me time to understand this table, and I had to do further tests to be sure. The first column is the result of the addition of the other 4 columns in that order, A+B+C+D . The operations are done starting from the left. If the NaN is not encountered in A or B then the NaN effectively "resets" the chain of additions to 0, so the result of the additions is whatever appears after that point. However, if the NaN occurs in the initial operation A+B in either position, then the result is 0 for that operation so the addition is whatever appears after the A+B point.
>> nan + int8(1) + int8(-5) + int8(-3)ans = -8
Entire first operation is affected, giving 0 for the pair so int8(1) is not contributing anything and the result is what is to the right of it
>> int8(0) + nan + int8(1) + int8(-5) + int8(-3)ans = -7
The first operation is affected, giving 0 for the first pair, but this is effectively the same as the case where the nan is encountered further on, with the nan resetting the total to 0 where it is encountered.
The operation of nan on items that are of single or double precision is well defined: of those were single or double items instead of uint8 or int8, then the nan would have propagated and the result would nan. But I do not recall seeing the result of combining nan and the integer classes documented.
I can come up with an explanation based upon pairwise operations and left-to-right evaluation. I would not say that it is wrong, just that it is unexpected . I was convinced for a while that it must be doing the operations right to left, until I tested further and found that it was just apparently asymmetric.
Best Answer