Find indices of elements in an array based on a search from another array

Viewed 85784

Imagine that i have two arrays:

a = [1, 2, 5, 7, 6, 9, 8, 3, 4, 7, 0];

b = [5, 9, 6];

I want to find the indices of the values of b in a (only the first hit) ie:

c = [3, 6, 5];

Is there an easy Matlab native way to do this without looping and searching.

I have tried to use find() with:

find(a == b)

and it would work if you did this:

for i = 1:length(b)
    index = find(a == b(i));
    c = [c, index(1)]
end

But it would be ideal for it to be easier then this.

8 Answers
Related