Indices of intersection of two arrays

#1

Suppose I have two arrays:

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

How do I get the indices of values in b that are in a and vice versa? I know intersection() returns the values, but I’m after the indices of the two arrays. Right now I’m doing

indices = [findall(x->x==i,a) for i in b]
10-element Array{Array{Int64,1},1}:
 [3]
 [4]
 [5]
 [6]
 [] 
 [] 
 [] 
 [] 
 [] 
 []

which doesn’t seem optimal. Any ideas? Thanks in advance!

#2

You could write a function that loops through the two sorted vectors pretty easily.

1 Like
#3
findall(x->x in b, a)

or

findall(in(b),a)
3 Likes
#4

Perfect, thanks!