Increase counter for each element in array
조회 수: 2 (최근 30일)
이전 댓글 표시
Hey,
given A = [1 1 3 4 5 6 6 7 7 7]. How could I return the sequence B =[1 2 1 1 1 1 2 1 2 3] such that each duplicate is counted and B is the same size as A?
I appreciate your help!
댓글 수: 0
채택된 답변
madhan ravi
2020년 6월 17일
ix = A(:) == unique(A);
B = nonzeros(cumsum(ix) .* ix)
댓글 수: 6
madhan ravi
2020년 6월 18일
I knew you would come up with that question. That’s why you should experiment with 'stable' option in unique function.
추가 답변 (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!