Find elements meeting any of a number of criteria

后端 未结 4 1545
花落未央
花落未央 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:47

    Divakar's answer is the way to go. But in case you want to do it more manually:

    [~, Output] = max(bsxfun(@eq, DJiSet(:).', JiSet(:)), [], 1);
    

    This finds the first occurrence if there are more than one.

    If the values in DJiSet were not guaranteed to be present in JiSet, you could use a small modification:

    [val, Output] = max(bsxfun(@eq, DJiSet(:).', JiSet(:))); %'
    Output(~val) = 0; %// 0 indicates "not found"
    

提交回复
热议问题