Classify numbers in a vector
이전 댓글 표시
Hello, I got a problem when dealing with numbers classifying in a vector. Problem is like that:
With a vector A = [1 2 3 4 5 9 10 13 15 17 18 19 21 24];
I try to separate the consecutive series and non-consecutive series so that I am able to have outputs like:
a=[1 2 3 4 5]
b=[9 10]
c=[13]
d=[15]
..etc
I tried diff() and regexp (), but I still can't solve it.
If anyone can help, it would be greatly appreciated.
Thank you!
채택된 답변
추가 답변 (1개)
Azzi Abdelmalek
2016년 1월 22일
편집: Azzi Abdelmalek
2016년 1월 22일
A = [8 1 2 3 4 5 9 10 13 15 17 18 19 21 22 24 25];
ii=[1 diff(A)];
idx1=[1 find(ii~=1)];
idx2=[idx1(2:end)-1 numel(A)];
out=arrayfun(@(x,y) A(x:y),idx1,idx2,'un',0);
celldisp(out)
카테고리
도움말 센터 및 File Exchange에서 Text Analytics Toolbox에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!