MATLAB: Output of the row of a vector which has the same entry as a second vector

MATLABprogramming find ismember

Hello,
Problem: Two vectors a and b. Id like to know in which row of the vector a, the entries of a and b are the same. Output vector r. if a entry in b occurs twice/triple/… the output of the row should also be twice/triple/…
I tried it like this:
a=[1 3 5 8 9 10 13 16]';
b=[2 5 1 1 8 8 8]';
r=find(ismember(a,b))
Solution: r =[1;3;4]
desired solution: r =[1;1;3;4;4;4]
Thanks in advance for your help!

Best Answer

You were close, but you want to switch the order of the arguments to ISMEMBER and use the two output call.
% Original data
a=[1 3 5 8 9 10 13 16]';
b=[2 5 1 1 8 8 8]';
% Get R
[H,R] = ismember(b,a);
R = sort(R(H)) % Do you really need it sorted??