In Julia I am trying to write a function that will (without copying) compute something from two vectors. As an example think of Pearson correlation (although it is not that). What I have got is:
function get_valid_entries(x::Vector, y::Vector)
valid_entries_in_x = .!(ismissing.(x)
valid_entries_in_y = .!(ismissing.(y)
return findall(valid_entries_in_x .& valid_entries_in_y)
end
x = [4.0, missing, 6.0, 5.0, 8.0, 10.0]
y = [2.0, 3.0, missing, 5.0, 6.0, 2.0]
valids = get_valid_entries(x, y)
function do_something(x::Vector{R}, y::Vector{R}) where R<:Real
println("This is do_something(x::Vector{R}, y::Vector{R})")
end
do_something(x[valids], y[valids])
This does not work as x[valids]
and y[valids]
are Vector::{Union{Missing,Float64}}
. I was hoping to avoid copies though so I don’t want to just convert the vectors with Vector{Float64}(x[valids])
.
Is there a way I can avoid a copy? I was hoping I could avoid copying the vector slices now that there are no missings and the data in there presumably doesn’t have to be rewritten.
The other question is if for a long vector if it is generally faster to do the copy (as the copied vector will be continuous in memory)