LED
LED

Reputation: 129

Removing entire rows with NaNs from a multidimensional array in Julia?

I am trying to translate the first answer in Remove NaN row from X array and also the corresponding row in Y from Python to Julia 0.5.0 without importing numpy. I can replicate the "removing the NaNs" part with:

x1 = x[!isnan(x)]

but only using that reduces the 2D array down to 1D, and I don't want that. What would be the Julia equivalent of numpy.any in this case? Or if there isn't an equivalent, how can I keep my array 2D and delete entire rows that contain NaNs?

Upvotes: 2

Views: 2977

Answers (1)

mbauman
mbauman

Reputation: 31342

You can find rows that contain 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; dims=2)
5×1 Matrix{Bool}:
  1
  0
  1
  0
  0

Then you can use the returned logical array as a mask into the first dimension, but we need to (1) make it one-dimensional (with vec) and (2) flip the 1s to 0s elementwise:

julia> A[.!vec(nanrows), :]
  3×4 Matrix{Float64}:
   0.429502  0.903061  0.864065  0.289931
   0.760145  0.767524  0.183373  0.145246
   0.101338  0.96232   0.26143   0.136931

Upvotes: 3

Related Questions