Finding all indices by ismember

后端 未结 5 993
野性不改
野性不改 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:50

    The solutions of Eitan T. and Daniel R answer your question in total. My solution is a convenient and simpler alternative if you are just interested in the elements which are common in both vectors, but NOT how they are related in means of ismember, e.g. you just want to filter your data for common elements:

    I would use the inversion of the opposite: setxor

    A = [5 3 4 2]; 
    B = [2 4 4 4 6 8];
    
    [~,iai,ibi] = setxor(A,B);   % elements which are not in common
    ia = 1:numel(A);
    ib = 1:numel(B);
    ia(iai) = [];                %indices of elements of B in A
    ib(ibi) = [];                %indices of elements of A in B
    

    Or just the same thing twice:

    [~,iai,ibi] = setxor(A,B);
    ia = setxor(1:numel(A),iai);
    ib = setxor(1:numel(B),ibi);
    

    returns in both cases the indices of the elements also existing in the respective other vector, so to say an implementation of ~isnotmember

    ia =
    
         3     4
    
    ib =
    
         1     2     3     4
    

提交回复
热议问题