How do i create a table with increments of occurrences of a value based on another column?
조회 수: 8 (최근 30일)
이전 댓글 표시
I have a vector as: [1,2,3,4,5,1,3,4,6,5,1,3]
How do i create another vector as: [1,1,1,1,1,2,2,2,1,2,3,3]
Since 1 is repeating 3 times so i replaced the 1st occurance of 1 by 1, second occurence by 2 and third occurence by 3. Similarly, i did for others.
댓글 수: 0
답변 (1개)
David Hill
2021년 6월 8일
x= [1,2,3,4,5,1,3,4,6,5,1,3];
y=unique(x);
z=ones(size(x));
for k=y
a=find(x==k);
z(a)=1:length(a);
end
댓글 수: 2
David Hill
2021년 6월 8일
k just goes through the y array ([1 2 3 4 5 6]), taking on each value one at a time.
참고 항목
카테고리
Help Center 및 File Exchange에서 NaNs에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!