我在单元格里有一个字符串名。我想把其他牢房里的名字和它比较一下。
例如,第一单元格的第一列与第二单元格的所有列。
在下面的示例中,单元格a和单元格b。如您所见,列3的许多元素匹配上面第3列的元素。我想一列一列地比较。
是否有任何方法不使用循环进行单元格比较?
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中的result.because列3,它匹配超过60%的元素。
发布于 2013-07-28 16:07:03
使用带有匿名函数和细胞/数组函数的同成员可能有助于解决您的需求:
如果您真的不需要保留行和列:
% 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'发布于 2013-07-28 16:11:24
你想比较一下单元格(cell1column1和cell2column1,cell1column2和cell2colun2……)吗?检查至少60%的匹配情况?order重要吗?(如果在两列中都有相同的名称,但在不同的行中,则可以)?
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')
endhttps://stackoverflow.com/questions/17908680
复制相似问题