Removing whole rows with NaN from multidimensional array in Julia?

I am trying to translate the first answer to Remove the NaN string from the X array, as well as the corresponding string in Y from Python to Julia 0.5.0 without importing numpy. I can reproduce "NaN removal" with:

x1 = x[!isnan(x)]

but only using this reduces the 2D array to 1D, and I don't want this. What will be the equivalent of Julia in this case numpy.any? Or, if there is no equivalent, how can I save my 2D array and delete whole lines containing NaN?

+2
source share
1 answer

You can find lines containing a NaN entry with any:

julia> A = rand(5, 4)
       A[rand(1:end, 4)] = NaN
       A
5ร—4 Array{Float64,2}:
   0.951717    0.0248771  0.903009    0.529702
   0.702505  NaN          0.730396    0.785191
 NaN           0.390453   0.838332  NaN
   0.213665  NaN          0.178303    0.0100249
   0.124465    0.363872   0.434887    0.305722

julia> nanrows = any(isnan(A), 2) # 2 means that we reduce over the second dimension
5ร—1 Array{Bool,2}:
 false
  true
  true
  true
 false

, :

julia> A[!vec(nanrows), :]
2ร—4 Array{Float64,2}:
 0.951717  0.0248771  0.903009  0.529702
 0.124465  0.363872   0.434887  0.305722
+2

Source: https://habr.com/ru/post/1670333/


All Articles