matlab怎么找出一串数中的连续的数字

比如【1 2 3 6 7 8 9 10 11 12】中最长的连续数组为【6 7 8 9】,怎么写代码输出这个数组呢

clc;clear
a  = [1 1 2 3 6 7 8 9 10 11 12 14 16 17 19];
b  = cell(0,0);
num = [];
ct = 1;
head = 1;
tail = 1;
while(ct<numel(a))
    head = ct;
    ct = ct+1;
    while(ct<=numel(a)&&(a(ct)-a(ct-1))==1)
        ct = ct+1;
    end
    tail = ct-1;
    b = [b;a(head:1:tail)'];
    num = [num; tail-head+1];
end
if(tail<numel(a))
    b = [b;a(tail+1)'];
    num = [num; 1];
end
fprintf('最长的连续数组是:\n')
disp(b{max(num)==num})
温馨提示:答案为网友推荐,仅供参考
相似回答