Search code examples
stringmatlabcell

Matlab, two string cell comparison


I have some string name in cell. I want to compare some name in other cell with it.

For example first column of first cell with all column of the second one.

in the following example cell a and cell b. as you see many element of column 3 match element of column 3 of the above one. I want to compare column by column.

Is there any way without using loop for cell comparison ?

 a=

'virutass'  'BrucePerens'   'RogerBrowne'   'keverets'
'armando'   'srivasta'      'riel'          'theo'
[]          'wichert'       'acme'          'rgb'
[]          'joey'          'aoliva'        'benc'
[]          'buffy'         'connolly'      'azz'
[]          'willow'        'itamar'        'dorward'
[]          'gnuchris'      'Kay'            'nuked'
[]          'edward'        'sab39'          'Yashy'
[]          'rcw'            'sad'           'idallen'
[]          'JHM'          'davem'            []
[]          'jgg'           'Blacky'          []



b=

 'sp'       'Erbo'         'connolly'     'draco'
'thomasd'   'miguel'       'joey'            []
'isenguard' 'mathieu'      'Kay'             []
'yole'      'fejj'         'acme'            []
'ewan'      'harinath'     'sad'             []

if more than 60% of elements is in one column the result is the column number. In this example column 3 of a is the result.because column 3 in b match it for more than %60 of elements.


Solution

  • Use of anonymous functions and cell/arrayfun with ismember function may help to resolve your needs:

    If you don't really need to keep rows and columns:

    % Remove empty cells to compare only strings
    emptyCells = @(x) cellfun(@isempty,x);
    a(emptyCells(a)) = [];
    b(emptyCells(b)) = [];
    
    % Get ids of element of b included in a
    id = cellfun(@(elem) ismember(elem, a), b);
    
    % Shows resutls
    b(id)
    

    If you really need to keep the comparaison between rows of your cells:

    nColumns = 4
    emptyCells = @(x) cellfun(@isempty,x);
    % get only the non-empty values of the cell
    getValue = @(dataCell) dataCell(not(emptyCells(dataCell)));
    
    % Compare for each columns, values of b(:,col) and a(:,col)
    isBinA = arrayfun(@(col) ismember(getValue(b(:,col)), getValue(a(:,col))), 1:nColumns, 'UniformOutput', 0);
    

    Here you get a cell with logical values, for example :

    isBinA{3}
    
    ans =
    
         1
         0
         1
         1
         1
    

    In the columns 3 of the cell b, you have 4 names which are included in the columns 3 of cell a:

    b(isBinA{3},3)
    
    ans = 
    
        'connolly'
        'Kay'
        'acme'
        'sad'