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

2 ビュー (過去 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 Center および File ExchangeLogical についてさらに検索

製品


リリース

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by