Один подход с unique
и histc
-
[unqA,~,id] = unique(A);
out = unqA(histc(id,1:max(id))>1)
Или использовать accumarray
вместо histc
-
out = unqA(accumarray(id(:),1)>1)
bsxfun
Или используйте -
out = unqA(sum(bsxfun(@eq,id(:),1:max(id(:)).'))>1)
Примеры трасс -
1) Числовые массивы случай -
>> A
A =
6 3 7 7 4 3 8 5 2 3 1
>> [unqA,~,id] = unique(A);
>> unqA(histc(id,1:max(id))>1)
ans =
3 7
>> unqA(accumarray(id(:),1)>1)
ans =
3 7
>> unqA(sum(bsxfun(@eq,id(:),1:max(id(:)).'))>1)
ans =
3 7
массивы 2) Сотовый случай -
>> A = {'apple','banana','apple','mango','ball','cat','banana','apple'};
>> [unqA,~,id] = unique(A);
>> unqA(histc(id,1:max(id))>1)
ans =
'apple' 'banana'
>> unqA(accumarray(id(:),1)>1)
ans =
'apple' 'banana'
>> unqA(sum(bsxfun(@eq,id(:),1:max(id(:)).'))>1)
ans =
'apple' 'banana'
Возможный дубликат http://stackoverflow.com/questions/5385651/determining-the-number-of-occurrences-of-each-unique-element-in-a-vector – gregswiss
@gregswiss: Мне нужен список а не как часто они встречаются. Кроме того, решения в связанном вопросе не применимы к нечисловым массивам. – texnic