MATLAB: How to find the indices of the repeating elements in an array with respect to another array

data manipulationecgintersectismembersignal processing

I am using MATLAB R2016a version.Here is an example which explains my question.
a=[1,2,3,3,4,5,5,7,8,2] ;
b=[1,2,3,3,2,4,5,5] ;
[x,y]=ismember(b,a);
The output y is :
y=[1,2,3,3,2,4,5,5]
Whereas,I want the output to be:
y=[1,2,3,4,10,5,6,7]
I want the elements of b to be expressed in terms of indices of a,including the repeating elements. In ismember the indices of the repeated elements are repeated.
Any suggestions?

Best Answer

Here is a simple for-loop approach:
a=[1,2,3,3,4,5,5,7,8,2] ;
b=[1,2,3,3,2 2 2,4,5,5 999] ; % see my comments above
y = zeros(size(b)) ;
for bu = unique(b)
ib = find(b==bu) ;
ia = find(a==bu) ;
n = min(numel(ia),numel(ib)) ;
y(ib(1:n)) = ia(1:n) ;
end
disp(y)