Find all unique values and replace it with new values

5 ビュー (過去 30 日間)
Elysi Cochin
Elysi Cochin 2022 年 6 月 6 日
コメント済み: Image Analyst 2022 年 6 月 7 日
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 日
findgroups()
But since you are using floating point numbers you really should use the third output of uniquetol() instead
  4 件のコメント
Elysi Cochin
Elysi Cochin 2022 年 6 月 7 日
okay sir will consider that
Image Analyst
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
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

Image Analyst
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)
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.)

カテゴリ

Help Center および File ExchangeData Type Conversion についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by