Reputation: 698
Is there an equivalent to Python's pop
? I have an array x
and a boolean array flag
of the same length. I would like to extract x[flag]
and be able to store it in a variable x_flagged
while at the same time remove them in place from x
.
x = rand(1:5, 100)
flag = x .> 2
x_flagged = some_function!(x, flag) # Now x would be equal to x[x .<= 2]
Upvotes: 0
Views: 268
Reputation: 19088
Try this one using deleteat!
julia> function pop_r!(list, y) t = list[y]; deleteat!( list, y ); t end
julia> x = rand(1:5, 100)
100-element Vector{Int64}
julia> flag = x .> 2
100-element BitVector
julia> pop_r!( x, flag )
60-element Vector{Int64}
julia> x
40-element Vector{Int64}
Upvotes: 2
Reputation: 14695
You can use splice!
with a bit of help from findall
:
julia> x_flagged = splice!(x, findall(flag))
59-element Vector{Int64}:
...
julia> size(x)
(41,)
splice!(a::Vector, indices, [replacement]) -> items
Remove items at specified indices, and return a collection containing the removed items.
Upvotes: 1