MATLAB: Associate index of equal element in two arrays

association

Good afternoon,
I would like to know how to associate values from an array A to the same value in an array B, without sorting the arrays.
For example, if A=[1;3;7;2;5] and B=[5;7;2;1;3]
I tried [row,col]=find(A==B) in a for loop but it returns "Empty matrix: 0-by-1"
At the end, I would like to know which index of B correspond to the current index of A.
Many thanks,
Matthieu

Best Answer

If I understand your Question correctly, the intersect function will do exactly what you want:
A=[1;3;7;2;5];
B=[5;7;2;1;3];
[C,ia,ib] = intersect(A,B, 'stable')
Result = [C ia ib]
Result =
1 1 4
3 2 5
7 3 2
2 4 3
5 5 1
The first column is the element, the second is the index of ‘A’ and the third, of ‘B’.