How to generate sequence numbers
Afficher commentaires plus anciens
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
];
Réponse acceptée
Plus de réponses (1)
Andrei Bobrov
le 21 Mai 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));
Catégories
En savoir plus sur Matrix Indexing dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!