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

2 views (last 30 days)
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!

Accepted Answer

Stephen23
Stephen23 on 3 Sep 2019
Edited: Stephen23 on 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.

More Answers (0)

Products


Release

R2019a

Community Treasure Hunt

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

Start Hunting!