I have a vector x
that contains “actual values” and a vector y
that contains indices (which are less than or equal to the length of x
). I want to create a new vector z
whose ith element is x[y[i]]
. I am looking for a one-liner to do this. I tried z = x.[y]
but that’s not a thing.
The code below does implement this but I was hoping for a better method.
For purposes of this post, I am trying to distill the issue, but this would be with columns in a dataframe and x
would be a multidimensional array representing a probability in each state (i.e. in each index combinination of x
). y
(and other variables) would be categorical variables giving the indices/states of interest.
x = [1, 2, 2, 5]
y = [1, 2, 3, 4]
z = zeros(length(y))
for entry in y
z[entry] = x[entry]
end
z
# result:
4-element Array{Float64,1}:
1.0
2.0
2.0
5.0