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

조회 수: 1 (최근 30일)
Jerki Jokne
Jerki Jokne 2019년 9월 3일
편집: Stephen23 2019년 9월 3일
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!

채택된 답변

Stephen23
Stephen23 2019년 9월 3일
편집: Stephen23 2019년 9월 3일
>> [~,~,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.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

제품


릴리스

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by