How to find same elements in a cell array?

조회 수: 8 (최근 30일)
Alessandro Cristini
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
Azzi Abdelmalek 2015년 7월 14일
You mean [1,2,4,5] and [1,4,2,5], and their repetitions (3 and 2, respectively)
Alessandro Cristini
Alessandro Cristini 2015년 7월 14일
Yes, sorry!

댓글을 달려면 로그인하십시오.

채택된 답변

Azzi Abdelmalek
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
Alessandro Cristini
Alessandro Cristini 2015년 7월 14일
편집: Alessandro Cristini 2015년 7월 14일
Thanks a lot! It is just what I wanted to do!!
But, in the case of one (or more) element is empty or doesn't agree with the size of the other one, this method doesn't work..
For example, suppose that the cell array "a" is composed as follows:
a={[1 2][1,2,4,5] [1,4,2,5] [1,2,4,5] [4,1,5,2] [1,2,4,5] [1,4,2,5][ ]}
In this case, how can I solve the above problem?
Thanks.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Andrei Bobrov
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
Alessandro Cristini
Alessandro Cristini 2015년 7월 14일
편집: Alessandro Cristini 2015년 7월 14일
I hadn't read this answer that solve my second question. Thanks for the help!

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Resizing and Reshaping Matrices에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by