# Creating a vector from a vector of values and a vector of indices to the first vector

**URL:** <https://discourse.julialang.org/t/creating-a-vector-from-a-vector-of-values-and-a-vector-of-indices-to-the-first-vector/55261>\
**Category:** New to Julia\
**Tags:** array\
**Created:** [February 14, 2021, 5:18pm UTC](https://discourse.julialang.org/t/creating-a-vector-from-a-vector-of-values-and-a-vector-of-indices-to-the-first-vector/55261 "2021-02-14T17:18:46Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![samerb](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/samerb/32/9242_2.png) [@samerb](https://discourse.julialang.org/u/samerb)\
**Post date:** [February 14, 2021, 5:18pm UTC](https://discourse.julialang.org/t/creating-a-vector-from-a-vector-of-values-and-a-vector-of-indices-to-the-first-vector/55261/1 "2021-02-14T17:18:46Z")

</div>

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.

```julia
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

```

---

<div class="post-metadata">

**Author:** ![grunst](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/grunst/32/9271_2.png) [@grunst](https://discourse.julialang.org/u/grunst)\
**Post date:** [February 14, 2021, 5:25pm UTC](https://discourse.julialang.org/t/creating-a-vector-from-a-vector-of-values-and-a-vector-of-indices-to-the-first-vector/55261/2 "2021-02-14T17:25:46Z")

</div>

```julia
julia> x[y]
4-element Array{Int64,1}:
 1
 2
 2
 5

```
