matlab 数据筛选找出一组数中连续等于零最长的数据

假如有一维矩阵数据 a=2 0 1 4 5 0 0 2 9 0 0 0 5
编写matlab程序找到连续等于O最长段,输出最长段中等于0个数和第一个0的位置,即long=3,pos=10

第1个回答  2010-07-18
a=[2 0 1 4 5 0 0 0 2 9 0 0 0 5];
num0=0;numSeq=[];zeroNum=[];
for ki=1:length(a)
if a(ki)==0
num0=num0+1;
if ki<length(a)
if a(ki+1)~=0
numSeq=[numSeq;num0];
num0=0;
end
elseif ki==length(a)
numSeq=[numSeq;num0];
num0=0;
end
if ki==1 && a(1)==0
zeroNum=[zeroNum;1];
end
if ki>2
if a(ki-1)~=0
zeroNum=[zeroNum;ki];
end
end

end
end
[C,I]=max(numSeq);
disp(C) % 最长0串长度
disp(zeroNum(I)) % 最长0串第一个0所在序号本回答被提问者采纳
第2个回答  2010-07-18
a=[2 0 1 4 5 0 0 2 9 0 0 0 5];
long=[];
pros=[];
p=1;
for i=1:length(a)
if a(i)==0
if a(i+1)==0
p=p+1;
else
long=[long;p];
pros=[pros;i-p+1];
p=1;
end
end
end
[a1 a2]=max(long);
pros(a1)
a2
相似回答