查找每个系列连续数字matlab的起点和终点

时间:2014-10-22 08:41:38

标签: matlab

比方说,我有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]。系列号是B=[1:24] 我的问题是如何找到每个连续数字的开始和结束。 应该是我答案的开始和结束点

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`

1 个答案:

答案 0 :(得分:1)

类似的东西:

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, [])'

或者现在您想要2列,这样做更容易(也更合乎逻辑):

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