How to generate sequence numbers
이전 댓글 표시
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
];
채택된 답변
추가 답변 (1개)
Andrei Bobrov
2017년 5월 21일
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));
카테고리
도움말 센터 및 File Exchange에서 Matrix Indexing에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!