How to find the number of consecutive occurrence of the numbers?

조회 수: 14 (최근 30일)
I'm in need to get the number of consecutive occurrence of the numbers in an array. Here is an example,
A=[1,2,3,1,2,3,1,1,1];
unqA=unique(A);
count=histc(A,unqA);
It give the count of 1 as 5, then 2 and 3 as 2. In the array A, the consecutive occurrence of 1 is 3 (i.e., maximum occurrence). Please help me how to get the value as 3 for the array A.(just for example).
Thanks in advance.

채택된 답변

Roger Stafford
Roger Stafford 2013년 5월 31일
It isn't clear if you are asking for the maximum consecutive occurrence for each possible number in A or the maximum of these maximum occurrences. Here is a way to find the latter.
p = find([true,diff(A)~=0,true]);
[c,q] = max(diff(p));
m = A(p(q));
The pair (m,c} gives the number occurring consecutively the largest number of times and the number of those times, respectively.

추가 답변 (2개)

Azzi Abdelmalek
Azzi Abdelmalek 2013년 5월 31일
A=[1,2,2,3,1,2,3,1,1,1];
ii=num2str(diff(A));
ii(strfind(ii,' '))=[];
jj=regexp(ii,'[^0*]','split');
jj(strcmp(jj,''))=[];
out=max(cellfun(@numel,jj))+1

per isakson
per isakson 2013년 5월 31일
Search File Exchange for runlength.
I think you can find solutions in Cody too.
  댓글 수: 3
Jan
Jan 2013년 5월 31일
편집: Jan 2013년 5월 31일
@Bless: On http://www.n-simon.de/mex you find download links for RunLength.mexw64 and RunLength.mexw32 for current Matlab versions, and RunLength.dll for Matlab versions before 7.6. All are compiled for Windows. If you use a Linux or Mac, you have to install a compiler and follow the shown instructions.
In addition you can use the M-file RunLength_M also.
Feel free to send me the complete error message by email and explain, why you do not get compiled MEX files from the download page.
Bless
Bless 2013년 5월 31일
Thank you Jan Simon..Now I got it..

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

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by