我在Matlab中有一个大的字符串数组。我需要在这个数组中找到重复字符串的索引。也就是说,我期望的输出是在字符串的单元格数组中出现两次或更多次的字符串索引数组。
我该怎么做?
答案 0 :(得分:7)
可以使用unique:
完成此操作strings = {'some' 'strings' 'with' 'with' 'duplicate' 'strings' 'strings'};
[~, uniqueIdx] =unique(strings) % Find the indices of the unique strings
duplicates = strings % Copy the original into a duplicate array
duplicates(uniqueIdx) = [] % remove the unique strings, anything left is a duplicate
duplicates = unique(duplicates) % find the unique duplicates
答案 1 :(得分:2)
您可以对数组进行排序,然后检查每个单元格是否等于以下单元格。
运行时= O(N log(N))
我不记得内置函数了。
Arr = ['aa' 'bb' 'cc' 'bb'];
ArrSort = sort(Arr);// Arr = ['aa' 'bb' 'bb' 'cc']
NewArr = ArrSort(1);
newInd = 1;
for i=2:length(ArrSort)
if NewArr(newInd) ~= ArrSort(i)
newInd = newInd + 1;
NewArr(newInd) = ArrSort(i)
end
end
答案 2 :(得分:2)