Relabel the arrays' elements with an increasing order

1 回表示 (過去 30 日間)
Sim
Sim 2020 年 10 月 11 日
コメント済み: Ameer Hamza 2020 年 10 月 11 日
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

採用された回答

Ameer Hamza
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
  2 件のコメント
Sim
Sim 2020 年 10 月 11 日
thanks a lot!
Ameer Hamza
Ameer Hamza 2020 年 10 月 11 日
I am glad to be of help!

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by