How to generate sequence numbers
Mostrar comentarios más antiguos
Consider matrix A as follows:
A = [
1
1
1
2
2
2
2
2
3
3
4
4
5
];
I want to generate a sequence numbers and reset these numbers wherever the ID changed.
B = [
1 1
2 1
3 1
1 2
2 2
3 2
4 2
5 2
1 3
2 3
1 4
2 4
1 5
];
Respuesta aceptada
Más respuestas (1)
Andrei Bobrov
el 21 de Mayo de 2017
out = ones(numel(A)+1,1);
ii = [true;diff(A)~=0];
jj = diff(find([ii;1]));
out(ii) = out(ii)-[0;jj(1:end-1)];
out = cumsum(out(1:end-1));
Categorías
Más información sobre Matrix Indexing en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!