I am trying to write a mutating function where the value passed as first argument mutates depending on the second one.
As an example, remove_when_zero_in_b
below should return the values in vector a
for those indexes where vector b
is not 0
.
"""filters 'a' when there is a zero in 'b'"""
function remove_when_zero_in_b!(a::AbstractVector, b::Vector{<:Real})
a = a[b .!= 0]
a
end
E.g.
x = [1.0, 2.0, 3.0, 4.0, 5.0]
y = [0, 1, 0, 2 , 0 ]
remove_when_zero_in_b!(x, y) # should mutate x
Then x
should be:
println(x)
2-element Vector{Float64}:
2.0
4.0
However, the function above does not mutate x
and remains as the initial vector with 5
elements.
What am I missing here? How would a function mutating x
so I obtain the desired result look like?