Relabel the arrays' elements with an increasing order
1 回表示 (過去 30 日間)
古いコメントを表示
Hi, I have this array:
A = [3 3 3 3 1 1 4 4 4 4 2 2 2]
and I need to relabel those elements with an increasing order, as follows:
B = [1 1 1 1 2 2 3 3 3 3 4 4 4]
Do you know any more compact solution than mine (here below)?
B = zeros(1,length(A));
[~,startunique] = unique(A);
c = sort(startunique);
for i = 1 : length(c)
B(find(A==A(c(i)))) = i;
end
0 件のコメント
採用された回答
Ameer Hamza
2020 年 10 月 11 日
編集済み: Ameer Hamza
2020 年 10 月 11 日
Try this
A = [3 3 3 3 1 1 4 4 4 4 2 2 2];
[~, ~, B] = unique(A, 'stable');
Result
>> B
B =
1
1
1
1
2
2
3
3
3
3
4
4
4
その他の回答 (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!