如何比较 table 列 matlab 中的文本字符串
How to compare text strings in a table colum matlab
如果我有一个 N×1 table 列,如何检测是否有任何行相同?
如果您只是想确定是否有重复的行,您可以使用unique
来执行此操作。您可以检查列中唯一值的数量,并将其与同一列中的元素总数 (numel
) 进行比较
tf = unique(t.Column) == numel(t.Column)
如果你想确定哪些行是重复的,你可以再次使用unique
但使用第三个输出然后使用accumarray
来计算每个值的出现次数,然后 select 那些出现不止一次的值。
[vals, ~, inds] = unique(t.Column, 'stable');
repeats = vals(accumarray(inds, 1) > 1);
% And to print them out:
fprintf('Duplicate value: %s\n', repeats{:})
如果您想要一个 true
/false
的逻辑向量来表示存在重复项的位置,您可以执行与上述类似的操作
[vals, ~, inds] = unique(t.Column, 'stable');
result = ismember(inds, find(accumarray(inds, 1) > 1));
或
[vals, ~, inds] = unique(t.Column, 'stable');
result = sum(bsxfun(@eq, inds, inds.'), 2) > 1;
更新
您可以结合上述两种方法来完成您想要的。
[vals, ~, inds] = unique(t.Column, 'stable');
repeats = vals(accumarray(inds, 1) > 1);
hasDupes = numel(repeats) > 0;
if hasDupes
for k = 1:numel(repeats)
fprintf('Duplicate value: %s\n', repeats{k});
fprintf(' Found at: ');
fprintf('%d ', find(strcmp(repeats{k}, t.Column)));
fprintf('\n');
end
end
如果我有一个 N×1 table 列,如何检测是否有任何行相同?
如果您只是想确定是否有重复的行,您可以使用unique
来执行此操作。您可以检查列中唯一值的数量,并将其与同一列中的元素总数 (numel
) 进行比较
tf = unique(t.Column) == numel(t.Column)
如果你想确定哪些行是重复的,你可以再次使用unique
但使用第三个输出然后使用accumarray
来计算每个值的出现次数,然后 select 那些出现不止一次的值。
[vals, ~, inds] = unique(t.Column, 'stable');
repeats = vals(accumarray(inds, 1) > 1);
% And to print them out:
fprintf('Duplicate value: %s\n', repeats{:})
如果您想要一个 true
/false
的逻辑向量来表示存在重复项的位置,您可以执行与上述类似的操作
[vals, ~, inds] = unique(t.Column, 'stable');
result = ismember(inds, find(accumarray(inds, 1) > 1));
或
[vals, ~, inds] = unique(t.Column, 'stable');
result = sum(bsxfun(@eq, inds, inds.'), 2) > 1;
更新
您可以结合上述两种方法来完成您想要的。
[vals, ~, inds] = unique(t.Column, 'stable');
repeats = vals(accumarray(inds, 1) > 1);
hasDupes = numel(repeats) > 0;
if hasDupes
for k = 1:numel(repeats)
fprintf('Duplicate value: %s\n', repeats{k});
fprintf(' Found at: ');
fprintf('%d ', find(strcmp(repeats{k}, t.Column)));
fprintf('\n');
end
end