总是在[~,b2(i)] 这种破折号上停下来 说 Expression or statement is incorrect--possibly unbalanced (, {, or [. 到底为啥呢
代码是教材COPY的, 不应该错啊
function [PIPxy]=PIPs(ys,nofPIPs,typeofdist,pflag)
l=length(ys);
xs=(1:l)';% Column vector with xs
PIP_points=zeros(l,1);% Binary indexation
PIP_points([1,l],1)=1;% One indicate the PIP points.The first two
PIPs are the first and the last observation.
Adjacents=zeros(l,2);
currentstate=2;% Initial PIPs
while currentstate<=nofPIPs
Existed_Pips=find(PIP_points);
currentstate=length(Existed_Pips);
locator=nan(l,currentstate);
for j=1:currentstate
locator(:,j)=abs(xs-Existed_Pips(j,1));
end
b1=zeros(1,l);b2=b1;
for i=1:l
[~,b1(i)]=min(locator(i,:));% Closer point
locator(i,b1(i))=nan; % Do not consider Closer point
[~,b2(i)]=min(locator(i,:));% 2nd Closer Point
Adjacents(i,1)=Existed_Pips(b1(i));%x-coordinates of the
closer point
Adjacents(i,2)=Existed_Pips(b2(i));%x-coordinates of the
2nd closer points
end
%% Calculate Distance
Adjx=Adjacents;
Adjy=[ys(Adjacents(:,1)),ys(Adjacents(:,2))];
Adjx(Existed_Pips,:)=nan;% Existed PIPs are not candidates for
new PIP.
Adjy(Existed_Pips,:)=nan;
if typeofdist==1
[D]=EDist(ys,xs,Adjx,Adjy);
elseif typeofdist==2
[D]=PDist(ys,xs,Adjx,Adjy);
else
[D]=VDist(ys,xs,Adjx,Adjy);
end
[~,Dmax]=max(D);
PIP_points(Dmax,1)=1;
currentstate=currentstate+1;
end
PIPxy=[Existed_Pips, ys(Existed_Pips)];
%% Plot
if pflag==1
plot(ys), hold on
plot(Existed_Pips,ys(Existed_Pips),'r*'),hold off
end
end
%% Distance measures
% Euclidean Distance
function [ED]=EDist(ys,xs,Adjx,Adjy)
ED=((Adjx(:,2)-xs).^2+(Adjy(:,2)-ys).^2).^(1/2)+. . .
((Adjx(:,1)-xs).^2+(Adjy(:,1)-ys).^2).^(1/2);
end
% Perpendicular Distance
function [PD]=PDist(ys,xs,Adjx,Adjy)
slopes=(Adjy(:,2)-Adjy(:,1))./(Adjx(:,2)-Adjx(:,1));
constants=Adjy(:,2)-slopes.*Adjx(:,2);
PD=abs(slopes.*xs-ys+constants)./(slopes.^2+1).^(1/2);
% line function: y=kx+m (1)
% the perpendicular distance (PD) from a point p(x1,y1) to a line
% is given by the following formula:
% PD=abs(k*x1-y1+m)/sqrt(k^2+1)
end
% Vertical Distance
function [VD]=VDist(ys,xs,Adjx,Adjy)
slopes=(Adjy(:,2)-Adjy(:,1))./(Adjx(:,2)-Adjx(:,1));
constants=Adjy(:,2)-slopes.*Adjx(:,2);
Yshat=slopes.*xs+constants;
VD=abs(Yshat-ys);
end
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
一周热门 更多>