How to find same elements in a cell array?
9 ビュー (過去 30 日間)
古いコメントを表示
Alessandro Cristini
2015 年 7 月 14 日
編集済み: Alessandro Cristini
2015 年 7 月 14 日
Hello all,
I have the following question:
I need to find the same elements and how many times they are repeated in a cell array. The elements are vectors of integers (e.g, [1,3,4,5]). Then, I'd like to find the repeated sequences and the count of their repetitions.
For example, suppose that the cell array (C) is composed as follows:
[1,2,4,5] [1,4,2,5] [1,2,4,5] [4,1,5,2] [1,2,4,5] [1,4,2,5]
Is there any smart solution to get the sequences [1,2,4,5] and [1,4,2,5], and their repetitions (3 and 2, respectively)?
Thanks in advance,
Ale
2 件のコメント
Azzi Abdelmalek
2015 年 7 月 14 日
You mean [1,2,4,5] and [1,4,2,5], and their repetitions (3 and 2, respectively)
採用された回答
Azzi Abdelmalek
2015 年 7 月 14 日
編集済み: Azzi Abdelmalek
2015 年 7 月 14 日
a={[1,2,4,5] [1,4,2,5] [1,2,4,5] [4,1,5,2] [1,2,4,5] [1,4,2,5]}
b=cell2mat(a')
[ii,jj,kk]=unique(b,'rows')
out=[ii accumarray(kk,1)]
The last column of out is the frequency
1 件のコメント
その他の回答 (1 件)
Andrei Bobrov
2015 年 7 月 14 日
編集済み: Andrei Bobrov
2015 年 7 月 14 日
a = {[1,2,4,5] [1,4,2,5] [1,2,4,5] [4,1,5,2,1] [1,2,4,5] [1,4, 2,5]};
a = a(:);
[a1,b,c] = unique(cellfun(@char,a,'un',0));
lo = histc(c,1:max(c));
loo = lo(:) > 1;
out = [a(b(loo)), num2cell(lo(loo))];
1 件のコメント
参考
カテゴリ
Help Center および File Exchange で Resizing and Reshaping Matrices についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!