I am trying to find the indices of elements in a vector that correspond to another vector, preferably without using loops. For example, my input might be:
DJ
Approach #1
You can avoid find
by reversing the places of DJiSet
and JiSet
inside ismember
and then use the second output that gives us the matching indices -
[~,out] = ismember(DJiSet,JiSet)
Approach #2
Loopy approach catering to the specific conditions set in the question could be tried out, not sure if this will be more efficient though -
intv_idx = zeros(1,numel(DJiSet));
intv_idx(1) = find(JiSet==DJiSet(1),1);
start = intv_idx(1)+1;
for k = 2:numel(DJiSet)
idx = find(JiSet(start:end)==DJiSet(k),1);
start = idx+start;
intv_idx(k) = idx;
end
out = cumsum(intv_idx);