countRepetitions

버전 1.2.0.0 (2.85 KB) 작성자: Johannes Keyser
Counts all repetitions of each elements' repeated occurrences within a vector.
다운로드 수: 643
업데이트 날짜: 2010/3/18

라이선스 보기

How many sequences of any repeated element exist in a vector (and how long are they)? Example below.

T = COUNTREPETITIONS(A) returns the total number of repetitions of any
element in A. The n-th element in T is the number of n-time-repetitions
of any element in A.

[T,D] = COUNTREPETITIONS(A) also returns a structure, containing a detailed
listing of all repetitions for each element in A.
D.elements just reflects unique(A). Cell array k in D.listing contains only
the repetitions of D.elements(k), using the same format as T.

Input is restricted to be a vector of type logical, numeric or char.
Matrices don't get rejected, but are interpreted as single vector, written
column-wise from left to right (so possibly not what you want).

Example:
For A = [0,1,0,0,1,1,1,42,42,42], [T,D] = COUNTREPETITIONS(A) yields:

T = [2 1 2 0 0 0 0 0 0 0], thus reporting 2x single elements, 1x a tuple,
and 2x a triple of repeated elements.

D = elements: [0 1 42]
listing: {3x1 cell} is also easily interpreted:

D.listing contains all element-wise repetitions in the same order
as indicated by D.elements:

D.listing{:}
ans =
1 1 0 0 0 0 0 0 0 0 (<=> there's 1x a single 0, and 1x "[0,0]")
ans =
1 0 1 0 0 0 0 0 0 0 (<=> there's 1x a single 1, and 1x "[1,1,1]")
ans =
0 0 1 0 0 0 0 0 0 0 (<=> there's 1x "[42,42,42]")

Thus if you are interested in the repetitions of a specific element, you
may easily use logical indexing. For example:
D.listing{D.elements == 42}, just yields [0 0 1 0 0 0 0 0 0 0].

인용 양식

Johannes Keyser (2024). countRepetitions (https://www.mathworks.com/matlabcentral/fileexchange/26957-countrepetitions), MATLAB Central File Exchange. 검색됨 .

MATLAB 릴리스 호환 정보
개발 환경: R2009b
모든 릴리스와 호환
플랫폼 호환성
Windows macOS Linux
카테고리
Help CenterMATLAB Answers에서 Set Operations에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!
버전 게시됨 릴리스 정보
1.2.0.0

Tried to improve on all of Jan Simons' criticisms:
more general output format, avoiding redundant checks, avoiding crashes on valid input, added H1-line
Adapted summaries and help section accordingly.

1.1.0.0

misunderstood tags, now corrected them ;-)

1.0.0.0