2017-01-10 58 views
2

我正在嘗試將Remove NaN row from X array and also the corresponding row in Y中的第一個答案從Python導入到Julia 0.5.0而不導入numpy。我可以複製「去掉NaN的」部分:從Julia中的多維數組中刪除整行的NaN?

x1 = x[!isnan(x)] 

但只使用降低了二維數組到1D,我不希望出現這種情況。在這種情況下,numpy.any的Julia等價物是什麼?或者如果沒有等價的話,我如何保存我的數組2D並刪除包含NaN的整個行?

回答

1

您可以查找包含NaN的條目以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