你如何根据条件简单地选择一个数组的子集?我知道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
Run Code Online (Sandbox Code Playgroud)
期望的输出:
[2, 4]
Run Code Online (Sandbox Code Playgroud)
观察到的输出:
[nothing, 2, nothing, 4]
Run Code Online (Sandbox Code Playgroud)
Lut*_*mak 11
您正在寻找filter
http://docs.julialang.org/en/release-0.4/stdlib/collections/#Base.filter
这是filter(x->x%2==0,[1,2,3,5])#anwers的示例
[2]
有元素操作符(以"."开头):
julia> [1,2,3,4] % 2 .== 0
4-element BitArray{1}:
false
true
false
true
julia> x = [1,2,3,4]
4-element Array{Int64,1}:
1
2
3
4
julia> x % 2 .== 0
4-element BitArray{1}:
false
true
false
true
julia> x[x % 2 .== 0]
2-element Array{Int64,1}:
2
4
julia> x .% 2
4-element Array{Int64,1}:
1
0
1
0
Run Code Online (Sandbox Code Playgroud)
您可以使用该find()函数(或.==语法)来完成此任务.例如:
julia> x = collect(1:4)
4-element Array{Int64,1}:
1
2
3
4
julia> y = x[find(x%2.==0)]
2-element Array{Int64,1}:
2
4
julia> y = x[x%2.==0] ## more concise and slightly quicker
2-element Array{Int64,1}:
2
4
Run Code Online (Sandbox Code Playgroud)
请注意.==元素操作的语法.另请注意,find()返回与条件匹配的索引.在这种情况下,匹配条件的索引与匹配条件的数组元素相同.但是对于更一般的情况,我们希望将find()函数放在括号中以表示我们正在使用它来从原始数组中选择索引x.
更新: @Lutfullah Tomak关于这个filter()功能的好点.我相信虽然这find()可以更快,更有效.(虽然我知道匿名函数应该在0.5版本中变得更好所以这可能会改变吗?)至少在我的试验中,我得到了:
x = collect(1:100000000);
@time y1 = filter(x->x%2==0,x);
# 9.526485 seconds (100.00 M allocations: 1.554 GB, 2.76% gc time)
@time y2 = x[find(x%2.==0)];
# 3.187476 seconds (48.85 k allocations: 1.504 GB, 4.89% gc time)
@time y3 = x[x%2.==0];
# 2.570451 seconds (57.98 k allocations: 1.131 GB, 4.17% gc time)
Run Code Online (Sandbox Code Playgroud)
Update2:对这篇文章的评论中的好点x[x%2.==0]比快x[find(x%2.==0)].
| 归档时间: |
|
| 查看次数: |
1597 次 |
| 最近记录: |