Count consecutive elements in an array
조회 수: 11 (최근 30일)
이전 댓글 표시
suppose i have an array like: A = [1 1 1 1 2 2 2 4 4 4 4 1 1 1 1 2]
I want the result in this form, B = [4 3 4 4 1].
or something like this will be best: B = [4 1; 3 2; 4 4; 4 1; 1 2]. First colums contains the number of entry and the second colums contains the entry itself.
Kindly help.
댓글 수: 0
채택된 답변
Mathieu NOE
2021년 10월 6일
hello
try this :
A = [1 1 1 1 2 2 2 4 4 4 4 1 1 1 1 2];
% A = [1 2 2 4 4 1 1 2];
%% main code
p=find(diff(A)~=0);
s1 = [p(1) diff(p) numel(A)-p(end)];
s2 = A(cumsum(s1));
B = [s1' s2'] % First colums contains the number of entry and the second colums contains the entry itself
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Matrix Indexing에 대해 자세히 알아보기
제품
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!