全部版块 我的主页
论坛 数据科学与人工智能 数据分析与数据科学 MATLAB等数学软件专版
1102 4
2015-12-11
:(  是个书上的代码,为啥运行有错


function [PATTERNS]=flags(ys, w, typeofdist, thor, pflag)
%% Data preparation
l=length(ys);
[Peaks,Bottoms]=RW(ys,w,0);
% Ignore locals identified the first days
Peaks(Peaks(:,2)<=20,:)=[];
Bottoms(Bottoms(:,2)<=20,:)=[];
P_Indx=Peaks(:,2);B_Indx=Bottoms(:,2);
P_Indx=[P_Indx ones(length(P_Indx),1)];
B_Indx=[B_Indx 2*ones(length(B_Indx),1)];
PB_Indx=sortrows([P_Indx;B_Indx]);
m=size(PB_Indx,1); % Number of locals
Pot.FlagBull=[1;2;1;2];
Pot.FlagBear=[2;1;2;1];
Pot.Index=zeros(m,1);
for i=1:m-3
if PB_Indx(i:i+3,2)==Pot.FlagBull
Pot.Index(i+3,1)=1; % Potential Bullish Flags
elseif PB_Indx(i:i+3,2)==Pot.FlagBear
Pot.Index(i+3,1)=2; % Potential Bearish Flags
end
end



%% FLAGS (BULLISH)
PFBullidx=find(Pot.Index==1);
j=0;% Counter
mn=length(PFBullidx);
if mn~=0
for i=1:mn
PC=zeros(4,3);% PerCase
PC(:,[1,2])=PB_Indx(PFBullidx(i,1)-3:PFBullidx(i,1),:);
PC(:,3)=ys(PC(:,1));
YS=[ys,(1:l)'];
if PC(1,1)-thor<=0
Flocalx=1;
else
Flocalx=PC(1,1)-thor;
end
YS2=YS(Flocalx:PC(1,1),:);
% Beginning of the prevailing trend
[~, Flocalx2 ]=min(YS2(:,1));



最后一行为啥说我可能少了括号啥的呀。。。。。???? 我是抄书上的额
二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

全部回复
2015-12-11 13:07:12
这个是另一个代码,同上,总是在[~,b2(i)]  这种破折号上停下来 说 Expression or statement is incorrect--possibly unbalanced (, {, or [.           到底为啥呢

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
二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

2015-12-11 13:07:18
这个是另一个代码,同上,总是在[~,b2(i)]  这种破折号上停下来 说 Expression or statement is incorrect--possibly unbalanced (, {, or [.           到底为啥呢

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
二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

2015-12-11 22:18:19
???
二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

2017-4-20 22:17:11
您好,请查看下站内消息。
二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

相关推荐
栏目导航
热门文章
推荐文章

说点什么

分享

扫码加好友,拉您进群
各岗位、行业、专业交流群