从Julia的多维数组中删除带有NaN的整行?

编程入门 行业动态 更新时间:2024-10-28 16:30:16
本文介绍了从Julia的多维数组中删除带有NaN的整行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在尝试翻译在不导入numpy的情况下,将X数组中的NaN行以及Y中的相应行从Python移至Julia 0.5.0.我可以复制删除NaN"部分:

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)]

但是只能使用它将2D数组减小到1D,我不希望那样.在这种情况下,numpy.any的Julia等效值是什么?或者,如果没有等效项,该如何保持数组2D并删除包含NaN的整个行?

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?

推荐答案

您可以使用 any :

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), 2) # 2 means that we reduce over the second dimension 5×1 Array{Bool,2}: false true true true false

然后,您可以将返回的逻辑数组用作第一维的掩码,但我们需要首先使其成为一维:

Then you can use the returned logical array as a mask into the first dimension, but we need to make it one-dimensional first:

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

更多推荐

从Julia的多维数组中删除带有NaN的整行?

本文发布于:2023-06-09 06:27:41,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/596840.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:多维   组中   Julia   NaN

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!