m = length(x);
x1=x(1:m-1);
x2=x(2:m);
indzer = find(x1.*x2
这个程序有点问题,它找的是零点左侧的点,有时候右侧的点会更接近零点一些
我改了一下,不知道改的好不好,俩百分号之间是我加的
function pzero=fz(x)
m = length(x);
x1=x(1:m-1);
x2=x(2:m);
indzer = find(x1.*x2<0);
%%
n=length(indzer);
for i=1:n
if abs(x(indzer(i)))>abs(x(indzer(i)+1))
indzer(i)=indzer(i)+1;
end
end
%%
if any(x == 0)
iz = find( x==0 );
indz = [];
if any(diff(iz)==1)
zer = x == 0;
dz = diff([0 zer 0]);
debz = find(dz == 1);
finz = find(dz == -1)-1;
indz = round((debz+finz)/2);
else
indz = iz;
end
indzer = sort([indzer indz]);
end
pzero=indzer;
There may has something wrong in the code!
if the input is x=[1 0 -2 -3 2 -5 3 0 2 0 0 5 ]
the reture value should be pzero=[2 5 7 8 10 11]
but using the code above, the reture value
indzer =
2 4 5 6 8 11
where is 10 ?? Obviously it is zero!
something wrong with my chinese input, Sorry!
I just make a little modification of the code above
here's my codes
function pickzero=fz(x)
m = length(x);
x1=x(1:m-1);
x2=x(2:m);
indzer = find(x1.*x2<=0);
n=length(indzer);
for i=1:n
if abs(x(indzer(i)))>abs(x(indzer(i)+1))
indzer(i)=indzer(i)+1;
end
end
pickzero=indzer