Search code examples
stringmatlabfindcell

Matlab, Find cell elements


I have two cell arrays of string A and B. All cells of B are also in A. I want to find the index of cell of B in A. Thanks.

Example:

A=
 'aaaa'
 'bbbb'
 'cccc'
 'dddd'
 'ffff'

B=
 'ffff'
 'aaaa'

ans=
  5
  1

or

ans=
  1
  5

Solution

  • use either intersect or ismember

    [~, idxInA] = intersect(A,B)
    

    or

    LocInA = find(ismember(A,B))