Relabel the arrays' elements with an increasing order

1 view (last 30 days)
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

Accepted Answer

Ameer Hamza
Ameer Hamza on 11 Oct 2020
Edited: Ameer Hamza on 11 Oct 2020
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

More Answers (0)

Categories

Find more on Matrices and Arrays in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!