MATLAB: How to generate sequence numbers

arraycell arraysfor loopif statementMATLABmatrix manipulation

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
];

Best Answer

>> N = diff(find([1;diff(A)~=0;1]));
>> cell2mat(arrayfun(@(n)(1:n).',N(:),'uni',0))
ans =
1
2
3
1
2
3
4
5
1
2
1
2
1
Related Question