Finding all indices by ismember

后端 未结 5 992
野性不改
野性不改 2021-02-04 11:18

This is what is described in one of the examples for ismember:

Define two vectors with values in common.

A = [5 3 4 2]; B = [2 4 4

5条回答
  •  鱼传尺愫
    2021-02-04 11:37

    This line will return all indices:

    F=arrayfun(@(x)(find(A(x)==B)),1:numel(A),'UniformOutput',false)
    

    Or a longer version, which might be easier to read:

    F=cell(numel(A),1);
    for x=1:numel(A)
      F{x}=find(A(x)==B);
    end
    

    find(A(x)==B) checks for all occurrences of A(x) in B. This is done for each element of the array, either using a for loop or arrayfun.

提交回复
热议问题