Recoding unique numbers to another number

1 回表示 (過去 30 日間)
Cside
Cside 2020 年 12 月 28 日
回答済み: Walter Roberson 2020 年 12 月 28 日
Hi I have this array of numbers [5 5 6 6 6 11 11 ]and would like to convert every unique number from 1 to n. For example, if there are 10 unique numbers, I would like to relabel them from 1 to 10, with the same number coded the same for eg [1 1 2 2 2 3 3]
The actual array is attached here. How may I code for this? Thanks so much!

採用された回答

Stephen23
Stephen23 2020 年 12 月 28 日
The simple and efficient MATLAB solution:
X = [5,5,6,6,6,11,11];
[~,~,Z] = unique(X,'stable')
Z = 7×1
1 1 2 2 2 3 3

その他の回答 (2 件)

KALYAN ACHARJYA
KALYAN ACHARJYA 2020 年 12 月 28 日
編集済み: KALYAN ACHARJYA 2020 年 12 月 28 日
data=[5 5 6 6 6 11 11];
counts=histc(data,unique(data));
data_result=repelem(1:length(unique(data)),counts)
Results:
data_result =
1 1 2 2 2 3 3

Walter Roberson
Walter Roberson 2020 年 12 月 28 日
findgroups(data)

カテゴリ

Help Center および File ExchangeMultidimensional Arrays についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by