计算scala数组

时间:2015-05-15 21:28:02

标签: arrays scala nan

例如:

scala> val my_array = Array(4,5,Double.NaN,6,5,6, Double.NaN)
my_array: Array[Double] = Array(4.0, 5.0, NaN, 6.0, 5.0, 6.0, NaN)

scala> my_array.count(_ == Double.NaN)
res13: Int = 0

我理解两个Double.NaN彼此并不相同

scala> Double.NaN == Double.NaN
res14: Boolean = false

因此,我得到了我得到的结果,但我找不到能告诉我Double.NaN的数量的函数,我错过了什么?

python中,行为将如下所示:

In [43]: import numpy as np

In [44]: a = np.array([5,np.nan,5,7,4,np.nan])

In [45]: np.isnan(a)
Out[45]: array([False,  True, False, False, False,  True], dtype=bool)

In [46]: np.isnan(a).sum()
Out[46]: 2

1 个答案:

答案 0 :(得分:4)

Double.isNan完成工作:

scala> val array = Array(4,5,Double.NaN,6,5,6, Double.NaN)
array: Array[Double] = Array(4.0, 5.0, NaN, 6.0, 5.0, 6.0, NaN)

scala> array.count(_.isNaN)
res0: Int = 2