Find elements meeting any of a number of criteria

后端 未结 4 1543
花落未央
花落未央 2020-12-19 05:27

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         


        
4条回答
  •  执念已碎
    2020-12-19 05:52

    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);
    

提交回复
热议问题