필터 지우기
필터 지우기

Increase counter for each element in array

조회 수: 3 (최근 30일)
Dario Walter
Dario Walter 2020년 6월 17일
댓글: Dario Walter 2020년 6월 18일
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!

채택된 답변

madhan ravi
madhan ravi 2020년 6월 17일
ix = A(:) == unique(A);
B = nonzeros(cumsum(ix) .* ix)
  댓글 수: 6
madhan ravi
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.
Dario Walter
Dario Walter 2020년 6월 18일
Well done Madhan :). Thanks a lot!

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

추가 답변 (0개)

카테고리

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

태그

제품


릴리스

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by