简体   繁体   English

查找每个序列连续编号的起点和终点Matlab

[英]Find start and end point for every series consecutive number matlab

Let say, I have A=[1 1 1 4 4 4 4 4 1 1 2 2 1 1 2 2 1 1 1 3 3 3 3 3] . 假设我有A=[1 1 1 4 4 4 4 4 1 1 2 2 1 1 2 2 1 1 1 3 3 3 3 3] The series number is B=[1:24] My question is how can I find the start and end for every consecutive number. 序列号是B=[1:24]我的问题是如何找到每个连续数字的起点和终点。 Should be start and end point of my answer is 应该是我回答的起点和终点是

for A=1 is 1,3;9,10;13,14;17,19
for A=2 is 11,12;15,16
for A=3 is 20,24
for A=4 is 4,8`

Something like: 就像是:

n = 1;
B = find(diff([0,A==n,0]));         %//Find where sequences of n and not  begin
B(2:2:end) = B(2:2:end) - 1       %//Change from the beginning of not n sequence to the end of the n sequence
reshape(B, 2, [])'

OR now that you want 2 columns it's easier (and more logical) to do this: 或者,现在您想要2列,这样做起来更容易(更合逻辑):

s = find(diff([0,A==n,0])==1);
e = find(diff([0,A==n,0])==-1) -1;
B = [s', e']

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM