Reputation: 197
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.
Upvotes: 0
Views: 227
Reputation: 90
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'
Upvotes: 1
Reputation: 78
Do you want to compare the cells columnwise(cell1column1 with cell2column1, cell1column2 with cell2colun2.....) and check for at least 60% match? Is order important (if the same name is in both columns, but different rows, is it OK)?
if length(intersect(a(:,1),b(:,1)))>0.6*length(b(:,1))
disp('Column 1 is good')
else
disp('Column 1 is bad')
end
if length(intersect(a(:,2),b(:,2)))>0.6*length(b(:,2))
disp('Column 2 is good')
else
disp('Column 2 is bad')
end
if length(intersect(a(:,3),b(:,3)))>0.6*length(b(:,3))
disp('Column 3 is good')
else
disp('Column 3 is bad')
end
if length(intersect(a(:,4),b(:,4)))>0.6*length(b(:,4))
disp('Column 4 is good')
else
disp('Column 4 is bad')
end
Upvotes: 1