Find all unique values and replace it with new values

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];

 채택된 답변

Walter Roberson
Walter Roberson 2022년 6월 6일

2 개 추천

findgroups()
But since you are using floating point numbers you really should use the third output of uniquetol() instead

댓글 수: 4

But since you are using floating point numbers you really should use the third output of uniquetol() instead
Sir, but why? unique and findgroups also give the result. What is the reason?
Walter Roberson
Walter Roberson 2022년 6월 6일
편집: Walter Roberson 2022년 6월 6일
Are you 100% positive that the second 0.23 will be bit-for-bit identical to the first one? Are you assigning the initial value to an variable and then copying it to different locations in the array? Because if there is the slightest difference in how they are created, they might be different.
4.9/10 - 0.49 = 5.5511e-17
okay sir will consider that

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

추가 답변 (2개)

KSSV
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
newV = 8×1
1 2 3 1 3 4 4 5
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)
g = 8×1
2 1 3 2 3 5 5 4
Tid = 5×1
0.1000 0.2300 0.3300 0.4000 0.6000
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.)

카테고리

도움말 센터File Exchange에서 Axis Labels에 대해 자세히 알아보기

태그

질문:

2022년 6월 6일

댓글:

2022년 6월 7일

Community Treasure Hunt

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

Start Hunting!

Translated by