Finding all indices by ismember

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

    A simple approach is to use bsxfun to test for equality between each element of A and B:

    ind = bsxfun(@eq, A(:), B(:).');
    list = cellfun(@find, mat2cell(ind, ones(1,numel(A)), numel(B)), 'uni', 0);
    

    The matrix ind gives the result in logical form (i.e. 0 or 1 values), and list is a cell array containing the indices:

    >>  ind
    
    ind =
    
         0     0     0     0     0     0
         0     0     0     0     0     0
         0     1     1     1     0     0
         1     0     0     0     0     0
    
    >> celldisp(list)
    
    list{1} =
    
         []
    
    
    list{2} =
    
         []    
    
    list{3} =
    
         2     3     4
    
    list{4} =
    
         1
    

提交回复
热议问题