0

What i have is 2 column vector variables/files each containing text entries. Lets say one is mx1 and other is nx1 (n>m). And the entries present in first file is present in second file but can be multiple times. So i want all those index, of second file at which the entries matches with first file. Example:

first file contains [ am pm cm dm]' and second file contains [am am bm pm pm pm dm em cm]'

So the answer will return as [1 2 4 5 6 7 9]'

Please help.

OR

If it can make a third file having only common entries like [am am pm pm pm dm cm]

user3077261
  • 133
  • 1
  • 8

1 Answers1

3

Just use ismember:

  1. Assuming your strings are contained in cell arrays:

    >> cell1 = {'am' 'pm' 'cm' 'dm'};
    cell2 = {'am' 'am' 'bm' 'pm' 'pm' 'pm' 'dm' 'em' 'cm'};
    find(ismember(cell2,cell1))
    
    ans =
    
         1
         2
         4
         5
         6
         7
         9
    
  2. If your strings are all equal-length and are defined as rows of text matrices:

    >> matrix1 = ['am';'pm';'cm';'dm'];
    matrix2 = ['am';'am';'bm';'pm';'pm';'pm';'dm';'em';'cm'];
    find(ismember(matrix2,matrix1,'rows'))
    
    ans =
    
         1
         2
         4
         5
         6
         7
         9
    
Luis Mendo
  • 110,752
  • 13
  • 76
  • 147