Recoding unique numbers to another number
3 次查看(过去 30 天)
显示 更早的评论
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!
0 个评论
采纳的回答
Stephen23
2020-12-28
The simple and efficient MATLAB solution:
X = [5,5,6,6,6,11,11];
[~,~,Z] = unique(X,'stable')
0 个评论
更多回答(2 个)
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
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Matrices and Arrays 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!