|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有账号?我要加入
x
各位前輩
我的程式碼運作效率很慢,我如何改寫為陣列式寫法呢,謝謝大家幫忙
function [Residual]=Residual(Data,Lag)
[n1]=size(Data,1) ;
[n2]= n1 -Lag;
[n3]= n1 -Lag-1;
[n4]= n1 -2*Lag;
One = zeros(n1,1);
One = One +1 ;
Estimates = zeros(n2,6);
Residual = zeros(n4,6);
ResidualStas = zeros(n4,8);
for i= 1 : (n1 - Lag)
y = Data(i:i+Lag,1);
x = [One(i:i+Lag,1) Data(i:i+Lag,2)];
[b,bint] = regress(y,x);
Estimates(i,1) = b(1,1);
Estimates(i,2) = b(2,1);
y = Data(i:i+Lag,2);
x = [One(i:i+Lag,1) Data(i:i+Lag,1)];
[b,bint] = regress(y,x);
Estimates(i,3) = b(1,1);
Estimates(i,4) = b(2,1);
if i > 1
Residual(i-1,1) = Data(i,1) - (Estimates(i-1,1) + Estimates(i-1,2)* Data(i,2));
Residual(i-1,2) = Data(i,2) - (Estimates(i-1,3) + Estimates(i-1,4)* Data(i,1));
end
if i >= Lag+1
ResidualStas(i - Lag ,1) = Residual(i-1,1);
ResidualStas(i - Lag ,2) = mean(Residual(i-Lag:i-1,1));
ResidualStas(i - Lag ,3) = std(Residual(i-Lag:i-1,1));
ResidualStas(i - Lag ,4) = Residual(i-1,2)
ResidualStas(i - Lag ,5) = mean(Residual(i-Lag:i-1,2));
ResidualStas(i - Lag ,6) = std(Residual(i-Lag:i-1,2));
ResidualStas(i - Lag ,7) = Estimates(i-1,2);
ResidualStas(i - Lag ,8) = Estimates(i-1,4);
end
end
Residual = ResidualStas; |
|