# Find position of Array elements in another Array

**URL:** <https://discourse.julialang.org/t/find-position-of-array-elements-in-another-array/39977>\
**Category:** General Usage\
**Tags:** indexing, arrays\
**Created:** [May 22, 2020, 5:16pm UTC](https://discourse.julialang.org/t/find-position-of-array-elements-in-another-array/39977 "2020-05-22T17:16:35Z")\
**Posts on this page:** 17\
**Page:** 1

<div class="post-metadata">

**Author:** ![sergevic](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sergevic/32/9958_2.png) [@sergevic](https://discourse.julialang.org/u/sergevic)\
**Post date:** [May 22, 2020, 5:16pm UTC](https://discourse.julialang.org/t/find-position-of-array-elements-in-another-array/39977/1 "2020-05-22T17:16:35Z")

</div>

Suppose I have the Array `a=[0.5, 0.6, 0.5, 0.4, 0.5, 0.6]` and a reference Array `b=[0.4, 0.5, 0.6]`. I want to to find the position in `b` of each element of `a`. I want then to obtain the output `[2, 3, 2, 1, 2, 3]`. I tried codes like ` findfirst.(x-> x in b,a)` or `findfirst.(x-> x.==b,a)` but they don’t work. I didn’t find any satisfactory solution in my seraches and attempts. Any hint, guys?

---

<div class="post-metadata">

**Author:** ![nboyd](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nboyd/32/13251_2.png) [@nboyd](https://discourse.julialang.org/u/nboyd)\
**Post date:** [May 22, 2020, 5:19pm UTC](https://discourse.julialang.org/t/find-position-of-array-elements-in-another-array/39977/2 "2020-05-22T17:19:12Z")

</div>

`findfirst.(isequal.(a), (b,))` should work.

---

<div class="post-metadata">

**Author:** ![pdeffebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pdeffebach/32/10320_2.png) [@pdeffebach](https://discourse.julialang.org/u/pdeffebach)\
**Post date:** [May 22, 2020, 5:20pm UTC](https://discourse.julialang.org/t/find-position-of-array-elements-in-another-array/39977/3 "2020-05-22T17:20:35Z")

</div>

```julia
julia> map(a) do ai
       findfirst(t -> t == ai, b)
       end

```

---

<div class="post-metadata">

**Author:** ![sergevic](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sergevic/32/9958_2.png) [@sergevic](https://discourse.julialang.org/u/sergevic)\
**Post date:** [May 22, 2020, 5:25pm UTC](https://discourse.julialang.org/t/find-position-of-array-elements-in-another-array/39977/4 "2020-05-22T17:25:04Z")

</div>

> [@nboyd](#):
>
> findfirst.(isequal.(a), (b,))

Gosh! I even tried `findfirst(isequal(a),b)` but completely forgot the element-wise operations! Didn’t know the `,` option after `b`.

---

<div class="post-metadata">

**Author:** ![sergevic](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sergevic/32/9958_2.png) [@sergevic](https://discourse.julialang.org/u/sergevic)\
**Post date:** [May 22, 2020, 5:25pm UTC](https://discourse.julialang.org/t/find-position-of-array-elements-in-another-array/39977/5 "2020-05-22T17:25:49Z")

</div>

Ok 🙂 Didn’t know about this application of `do`.

---

<div class="post-metadata">

**Author:** ![tbeason](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tbeason/32/15898_2.png) [@tbeason](https://discourse.julialang.org/u/tbeason)\
**Post date:** [May 22, 2020, 5:29pm UTC](https://discourse.julialang.org/t/find-position-of-array-elements-in-another-array/39977/6 "2020-05-22T17:29:19Z")

</div>

> [@sergevic](#):
>
> Didn’t know the `,` option after `b` .

Just a note on this because it is tricky and does come up. The reason he wrote `(b,)` is that if you do not wrap the `b`, Julia tries to also broadcast the `findfirst` function over `b` as well. You don’t want that in this case, so you have to “hide” `b` within something. He put `b` in a tuple, but you could also use `[b]` or `Ref(b)`.

---

<div class="post-metadata">

**Author:** ![rdeits](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rdeits/32/286_2.png) [@rdeits](https://discourse.julialang.org/u/rdeits)\
**Post date:** [May 22, 2020, 5:29pm UTC](https://discourse.julialang.org/t/find-position-of-array-elements-in-another-array/39977/7 "2020-05-22T17:29:27Z")

</div>

> [@sergevic](#):
>
> Didn’t know the `,` option after `b` .

That’s just creating a 1-element tuple:

```julia
julia> t = (1,)
(1,)

julia> typeof(t)
Tuple{Int64}

```

You need the `,` because otherwise `(x)` is just the same as `x`.

You can also do `Ref(b)` instead, since Julia guarantees that a `Ref` is treated as a scalar in broadcasting.

---

<div class="post-metadata">

**Author:** ![tbeason](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tbeason/32/15898_2.png) [@tbeason](https://discourse.julialang.org/u/tbeason)\
**Post date:** [May 22, 2020, 5:29pm UTC](https://discourse.julialang.org/t/find-position-of-array-elements-in-another-array/39977/8 "2020-05-22T17:29:52Z")

</div>

Gotcha by 2 secs 😉

---

<div class="post-metadata">

**Author:** ![sergevic](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sergevic/32/9958_2.png) [@sergevic](https://discourse.julialang.org/u/sergevic)\
**Post date:** [May 22, 2020, 5:31pm UTC](https://discourse.julialang.org/t/find-position-of-array-elements-in-another-array/39977/9 "2020-05-22T17:31:33Z")

</div>

Ok, perfect!

---

<div class="post-metadata">

**Author:** ![sergevic](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sergevic/32/9958_2.png) [@sergevic](https://discourse.julialang.org/u/sergevic)\
**Post date:** [May 22, 2020, 5:32pm UTC](https://discourse.julialang.org/t/find-position-of-array-elements-in-another-array/39977/10 "2020-05-22T17:32:30Z")

</div>

Ok got it 🙂

---

<div class="post-metadata">

**Author:** ![sergevic](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sergevic/32/9958_2.png) [@sergevic](https://discourse.julialang.org/u/sergevic)\
**Post date:** [May 22, 2020, 5:42pm UTC](https://discourse.julialang.org/t/find-position-of-array-elements-in-another-array/39977/11 "2020-05-22T17:42:35Z")

</div>

For the curious ones: the goal of the output Array `[2, 3, 2, 1, 2, 3]` is to apply it to a multidimensional Array like `A=rand(5,3,6)`. I want to select the column `2` in the first element of `A`, the column `3` in the second element of `A` and so on 😉 That part of code is ok for me 🙂 Just wanted to share the utility of obtaining such an output Array.

---

<div class="post-metadata">

**Author:** ![mcabbott](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mcabbott/32/6603_2.png) [@mcabbott](https://discourse.julialang.org/u/mcabbott)\
**Post date:** [May 22, 2020, 5:50pm UTC](https://discourse.julialang.org/t/find-position-of-array-elements-in-another-array/39977/12 "2020-05-22T17:50:24Z")

</div>

There is also `indexin(a, b)`.

---

<div class="post-metadata">

**Author:** ![sergevic](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sergevic/32/9958_2.png) [@sergevic](https://discourse.julialang.org/u/sergevic)\
**Post date:** [May 22, 2020, 5:55pm UTC](https://discourse.julialang.org/t/find-position-of-array-elements-in-another-array/39977/13 "2020-05-22T17:55:06Z")

</div>

OMG! Such a simple solution and command! Didn’t know the existence of this function.

---

<div class="post-metadata">

**Author:** ![mcabbott](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mcabbott/32/6603_2.png) [@mcabbott](https://discourse.julialang.org/u/mcabbott)\
**Post date:** [May 22, 2020, 5:57pm UTC](https://discourse.julialang.org/t/find-position-of-array-elements-in-another-array/39977/14 "2020-05-22T17:57:57Z")

</div>

This and `invperm` I’ve probably re-written with `findfirst` more often than I remember…

BTW I think `indexin` may be more efficient if `b` is large, as it scans just once. But not here.

---

<div class="post-metadata">

**Author:** ![sergevic](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sergevic/32/9958_2.png) [@sergevic](https://discourse.julialang.org/u/sergevic)\
**Post date:** [May 22, 2020, 6:09pm UTC](https://discourse.julialang.org/t/find-position-of-array-elements-in-another-array/39977/15 "2020-05-22T18:09:16Z")

</div>

Ok 🙂 Yes, `b` is not large.

---

<div class="post-metadata">

**Author:** ![bjarthur](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bjarthur/32/9638_2.png) [@bjarthur](https://discourse.julialang.org/u/bjarthur)\
**Post date:** [February 1, 2023, 2:28pm UTC](https://discourse.julialang.org/t/find-position-of-array-elements-in-another-array/39977/16 "2023-02-01T14:28:34Z")

</div>

`indexin` should be the accepted answer! @sergevic can you please change it?

---

<div class="post-metadata">

**Author:** ![rocco\_sprmnt21](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rocco_sprmnt21/32/20127_2.png) [@rocco\_sprmnt21](https://discourse.julialang.org/u/rocco_sprmnt21)\
**Post date:** [February 1, 2023, 2:59pm UTC](https://discourse.julialang.org/t/find-position-of-array-elements-in-another-array/39977/17 "2023-02-01T14:59:16Z")

</div>

```julia
d=Dict(b.=>eachindex(b))
map(e->get(d,e,nothing),a)

```

PS

> [@bjarthur](#):
>
> `indexin` should be the accepted answer

👆
