How to get vectors of common duplicates in a cell array?

2 vues (au cours des 30 derniers jours)
Jerki Jokne
Jerki Jokne le 3 Sep 2019
Modifié(e) : Stephen23 le 3 Sep 2019
Let's say I have a cell array of values, some of which are duplicates:
A = {'a', 'b', 'c', 'c', 'a', 'a'}
How can I get an output of the duplicate index values, grouped together? Should look something like this:
duplicates = {[1,5,6], [3,4]}
Thanks!

Réponse acceptée

Stephen23
Stephen23 le 3 Sep 2019
Modifié(e) : Stephen23 le 3 Sep 2019
>> [~,~,X] = unique(A);
>> [B,Y] = hist(X,1:max(X));
>> C = arrayfun(@(y)find(X==y),Y(B>1),'uni',0);
>> C{:}
ans =
1 5 6
ans =
3 4
Or
>> [~,~,X] = unique(A);
>> V = 1:numel(A);
>> C = accumarray(X(:),V(:),[],@(v){v});
>> C(cellfun(@isscalar,C)) = [];
>> C{:}
ans =
1
5
6
ans =
3
4
Or a simple loop.

Plus de réponses (0)

Catégories

En savoir plus sur Logical dans Help Center et File Exchange

Produits


Version

R2019a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by