# Indices of intersection of two arrays

**URL:** <https://discourse.julialang.org/t/indices-of-intersection-of-two-arrays/23043>\
**Category:** General Usage\
**Tags:** arrays\
**Created:** [April 11, 2019, 4:07pm UTC](https://discourse.julialang.org/t/indices-of-intersection-of-two-arrays/23043 "2019-04-11T16:07:01Z")\
**Posts on this page:** 6\
**Page:** 2

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [December 28, 2021, 10:51am UTC](https://discourse.julialang.org/t/indices-of-intersection-of-two-arrays/23043/21 "2021-12-28T10:51:31Z")

</div>

You might be using an earlier version of the function I posted in deleted post. Please check again.

---

<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:** [December 28, 2021, 11:01am UTC](https://discourse.julialang.org/t/indices-of-intersection-of-two-arrays/23043/22 "2021-12-28T11:01:56Z")

</div>

Another way to take into account the cases of multiple occurrences and to keep the correspondence of the position of the common value in the two vectors

```julia
function intersectalajulia3(a,b)
    ab=intersect(a,b)
    ia = [findall(==(e), a) for e in ab]
    ib = [findall(==(e), b) for e in ab]
    return Dict(zip(ab, zip(ia,ib)))
end

```

The data can be organized in different ways according to the use to be made of it.

```julia

a = rand(1:10, 20)
b = rand(1:15, 30)

function intersectalajulia4(a,b)
    ab=intersect(a,b)
    ia = [findall(==(e), a) for e in ab]
    ib = [findall(==(e), b) for e in ab]
    return hcat(ab, ia,ib)
end
m=intersectalajulia4(a,b)

using IndexedTables

function intersectalajulia5(a,b)
    ab=intersect(a,b)
    ia = [findall(==(e), a) for e in ab]
    ib = [findall(==(e), b) for e in ab]
    return ndsparse((comval=ab,), (whereina=ia,whereinb=ib))
end

it=intersectalajulia5(a,b)
it[keys(it)[2]]

```

```julia
julia> m=intersectalajulia4(a,b)
8×3 Matrix{Any}:
  4 [1, 12] [6, 9]
  6 [3, 8, 16] [1, 10, 28]
  9 [4, 13] [14, 24]
  8 [5, 9, 14, 15] [12, 16]
  7 [6, 20] [11]
 10 [7, 19] [2, 8]
  5 [10] [3, 22]
  1 [11, 18] [4, 17, 30]

julia> it=intersectalajulia5(a,b)
1-d NDSparse with 8 values (2 field named tuples):
comval │ whereina whereinb
───────┼────────────────────────────
1 │ [11, 18] [4, 17, 30]
4 │ [1, 12] [6, 9]
5 │ [10] [3, 22]
6 │ [3, 8, 16] [1, 10, 28]
7 │ [6, 20] [11]
8 │ [5, 9, 14, 15] [12, 16]
9 │ [4, 13] [14, 24]
10 │ [7, 19] [2, 8]

```

---

<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:** [December 28, 2021, 7:15pm UTC](https://discourse.julialang.org/t/indices-of-intersection-of-two-arrays/23043/24 "2021-12-28T19:15:17Z")

</div>

The following algorithm is more efficient for large vectors with intersection also of large size.

```julia

function intersectalajulia6(a, b)
    cv=Dict{Int64, NTuple{2, Vector{Int64}}}()
    for (i,e) in enumerate(a)
        push!(get!(()->(Int[],Int[]), cv, e)[1], i)
    end
    for (i,e) in enumerate(b)
        push!(get!(()->(Int[],Int[]), cv, e)[2], i)
    end
    filter(e->sum(isempty.(e[2]))==0 ,cv)
end

```

---

<div class="post-metadata">

**Author:** ![Deye\_Gvwargiya](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/deye_gvwargiya/32/43511_2.png) [@Deye\_Gvwargiya](https://discourse.julialang.org/u/Deye_Gvwargiya)\
**Post date:** [October 13, 2022, 3:06am UTC](https://discourse.julialang.org/t/indices-of-intersection-of-two-arrays/23043/25 "2022-10-13T03:06:39Z")

</div>

(For numeric vectors; Just a code translation from MATLAB)

First, let there be a ismember():

```julia
function ismember(a::Vector{T}, b::Vector{T}) where T <: Real
    locb = indexin(a, b);
    lia = [isnothing(e) ? false : true for e in locb];
    locb = convert(Vector{Int64}, replace(locb, nothing=>0::eltype(b)));
    return (lia, locb)
end

```

Then we can do:

```julia
function intersectidx(a::Vector{T}, b::Vector{T}) where T <: Real
    if length(a) >= length(b)
        (tf, ib) = ismember(a, b);
        ia = findall(tf .== true);
        ib = ib[ia];

        iau = uniqueidx(a[ia]);
        c = a[ia][iau];
        ia = ia[iau];
        ib = ib[iau];
    else
        (tf, ia) = ismember(b, a);
        ib = findall(tf .== true);
        ia = ia[ib];

        ibu = uniqueidx(b[ib]);
        c = b[ib][ibu];
        ia = ia[ibu];
        ib = ib[ibu];
    end
    return (c, ia, ib)
end

```

---

<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:** [June 3, 2023, 9:17am UTC](https://discourse.julialang.org/t/indices-of-intersection-of-two-arrays/23043/26 "2023-06-03T09:17:51Z")

</div>

indexin() is less known than it deserves

```julia
a = [2, 4, 6, 7, 10, 11]
b = [6, 7, 10, 11, 13, 15, 17, 19, 21, 23]

julia> indexin(b,a)
10-element Vector{Union{Nothing, Int64}}:
 3
 4
 5
 6
  nothing
  nothing
  nothing
  nothing
  nothing
  nothing

julia> indexin(a,b)
6-element Vector{Union{Nothing, Int64}}:
  nothing
  nothing
 1
 2
 3
 4

```

---

<div class="post-metadata">

**Author:** ![Andrea\_Vigliotti](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/andrea_vigliotti/32/7635_2.png) [@Andrea\_Vigliotti](https://discourse.julialang.org/u/Andrea_Vigliotti)\
**Post date:** [April 19, 2024, 2:09pm UTC](https://discourse.julialang.org/t/indices-of-intersection-of-two-arrays/23043/27 "2024-04-19T14:09:41Z")

</div>

that is why Julia is so elegant.

[Previous page](https://discourse.julialang.org/t/indices-of-intersection-of-two-arrays/23043.md?page=1)
