How to generate sequence numbers
Show older comments
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
];
Accepted Answer
More Answers (1)
Andrei Bobrov
on 21 May 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));
Categories
Find more on Matrix Indexing 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!