Find all unique values and replace it with new values
조회 수: 5 (최근 30일)
이전 댓글 표시
I have a vector with values
v = [0.23; 0.1; 0.33; 0.23 ; 0.33; 0.6; 0.6; 0.4];
I would like to find the unique values and replace the vector with numbers from 1, like
newV = [1; 2; 3; 1 ; 3; 4; 4; 5];
댓글 수: 0
채택된 답변
Walter Roberson
2022년 6월 6일
findgroups()
But since you are using floating point numbers you really should use the third output of uniquetol() instead
댓글 수: 4
Image Analyst
2022년 6월 7일
@Elysi Cochin I'm pretty sure you've seen the FAQ on this before but here it is again:
추가 답변 (2개)
KSSV
2022년 6월 6일
v = [0.23; 0.1; 0.33; 0.23 ; 0.33; 0.6; 0.6; 0.4];
[c,ia,newV] = unique(v,'stable') ;
newV
댓글 수: 0
Image Analyst
2022년 6월 6일
Will this do:
v = [0.23; 0.1; 0.33; 0.23 ; 0.33; 0.6; 0.6; 0.4];
% newV = [1; 2; 3; 1 ; 3; 4; 4; 5];
[g, Tid] = findgroups(v)
I know it doesn't match your desired but it's easy and maybe you don't need that exact order. If you need that exact order, can you explain why you need that order? (And don't jsut say I need it because I need it.)
참고 항목
카테고리
Help Center 및 File Exchange에서 Data Type Conversion에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!