Top Menu

Pages

Thursday, June 20, 2013

Matlab tip -- finding the indices of the elements of one array in another

Suppose all elements in array1 are contained in array2, now we want to get the indices of array1's elements in array2.

Here is a simple and neat solution, using Matlab's arrayfun function, which applies a function to each element of an array.
array1=[1,6,2,3,7];
array2=[3,6];
arrayfun(@(x)find(array1==x,1),array2)
The output is:
ans =
      4    2

No comments:

Post a Comment