String Matlab,两串单元比较
我的单元格中有一些字符串名称。我想把其他单元格中的某个名称与它进行比较 例如,第一个单元格的第一列和第二个单元格的所有列 在下面的示例中,单元格a和单元格b。正如您所看到的,第3列的许多元素与上面一列中第3列的元素相匹配。我想逐列比较 有没有不使用循环进行单元格比较的方法String Matlab,两串单元比较,string,matlab,cell,String,Matlab,Cell,我的单元格中有一些字符串名称。我想把其他单元格中的某个名称与它进行比较 例如,第一个单元格的第一列和第二个单元格的所有列 在下面的示例中,单元格a和单元格b。正如您所看到的,第3列的许多元素与上面一列中第3列的元素相匹配。我想逐列比较 有没有不使用循环进行单元格比较的方法 a= 'virutass' 'BrucePerens' 'RogerBrowne' 'keverets' 'armando' 'srivasta' 'riel' 'theo' []
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' []
如果一列中有60%以上的元素,则结果为列号。在本例中,a的第3列是结果。因为b中的第3列与超过60%的元素匹配。使用函数和函数可能有助于解决您的需要:
如果您真的不需要保留行和列:
% 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)
如果确实需要保持单元格行之间的比较:
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);
这里您可以得到一个具有逻辑值的单元格,例如:
isBinA{3}
ans =
1
0
1
1
1
在单元格b的第3列中,有4个名称包含在单元格a的第3列中:
b(isBinA{3},3)
ans =
'connolly'
'Kay'
'acme'
'sad'
是否要按列比较单元格(Cell1列1与Cell2列1、Cell1列2与Cell2列2…)并检查至少60%的匹配?顺序重要吗(如果两列中的名称相同,但行不同,可以吗)
你能从上面的单元格中举出一个例子吗?随便挑name@P0W谢谢我编辑了问题。@Fatime结果如何?您希望这个特定示例的结果是什么样子?@EitanT感谢您的评论。我编辑了它。请按照这个类似的问题。
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