您如何简单地根据条件选择数组的子集?我知道Julia不使用向量化,但是必须有一种简单的方法来进行以下操作,而又不会出现难看的多行for
循环
julia> map([1,2,3,4]) do x
return (x%2==0)?x:nothing
end
4-element Array{Any,1}:
nothing
2
nothing
4
所需的输出:
[2, 4]
观察到的输出:
[nothing, 2, nothing, 4]
您正在寻找filter
http://docs.julialang.org/en/release-0.4/stdlib/collections/#Base.filter
这是一个filter(x->x%2==0,[1,2,3,5])
#anwers与[2]
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句