Relabel the arrays' elements with an increasing order

1 visualización (últimos 30 días)
Sim
Sim el 11 de Oct. de 2020
Comentada: Ameer Hamza el 11 de Oct. de 2020
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

Respuesta aceptada

Ameer Hamza
Ameer Hamza el 11 de Oct. de 2020
Editada: Ameer Hamza el 11 de Oct. de 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

Más respuestas (0)

Categorías

Más información sobre Matrices and Arrays en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by