在使用matlab以下两组函数时请务必注意:
- sum 和 nansum
- mean 和 nanmean
这两个的区别相对好理解,后者其实是指将矩阵中的NaN(空值去掉之后)再进行求解和平均;但我想说的是,注意!
Note: If X contains a vector of all NaN values along some dimension, the vector is empty once the NaN values are removed, so the sum of the remaining elements is 0. The output 0 is not a sum of NaN values.
data =
1 NaN NaN
NaN 2 NaN
>> nansum(data)
ans =
1 2 0
>> sum(data)
ans =
NaN NaN NaN
data =
1 NaN NaN
NaN 2 NaN
>> nanmean(data)
ans =
1 2 NaN
>> mean(data)
ans =
NaN NaN NaN
【敲黑板】
-
如果数据的某一维全是 NaN, 则sum计算的结果当然仍然是NaN, 但是nansum计算的结果却是0,请勿小看这个0,如果不注意,会直接影响结果!
-
对于mean 和 nanmean,上述情况存在时,结果都是NaN