工作,最近我詢問了相關的問題在這裏:程序正常運行在Matlab,但不會在並行工具箱
https://stackoverflow.com/questions/21171836/storing-matrix-output-in-higher-dimensional-matrix
我現在只是想運行在一個並行結構的代碼,但是當我我是否收到以下錯誤:
???錯誤使用==> parallel_function在598 錯誤在==> OIRE在136 未定義的函數或變量「best_index」。
錯誤==> OIRE_MSE_test在73 PARFOR t = 1時:nsims
在==錯誤> OIRE_MSE_test在95 [b_OIRE OIRE_opt_b(:,:,T)] = OIRE(Y,X, ITER);
的代碼,只要我把MATLAB池開/關和PARFOR命令工作正常。 這怎麼會不會在並行工具箱中運行?
clc;
n=100;
p=7;
alpha1=0.999999;
error_vol=0.1;
iter=1000;
nsims=200;
OIRE_opt_b=zeros(7,3,nsims);
OIRE_opt_MSE=zeros(1,3,nsims);
OIRE_opt_index=zeros(1,3,nsims);
GIREI_opt_b=zeros(7,3,nsims);
GIREI_opt_MSE=zeros(1,3,nsims);
GIREI_opt_index=zeros(1,3,nsims);
GIREII_opt_b=zeros(7,3,nsims);
GIREII_opt_MSE=zeros(1,3,nsims);
GIREII_opt_index=zeros(1,3,nsims);
LRRE_opt_b=zeros(7,3,nsims);
LRRE_opt_MSE=zeros(1,3,nsims);
LRRE_opt_index=zeros(1,3,nsims);
matlabpool open
x=zeros(n,p);
for i=1:n
z_i4=normrnd(0,1);
x(i,p)=z_i4;
for j=1:p-1
x(i,j)=x(i,j)+alpha1*z_i4;
x(i,j)= x(i,j)+(1-alpha1^2)^(0.5)*normrnd(0,1);
end
end
b_act=[5;1;10;-20;200;30;-2];
parfor t=1:nsims
residuals=normrnd(0,error_vol,n,1);
y=x*b_act + residuals;
y_store(:,t)=y;
y=y_store(:,t);
[b_OIRE OIRE_opt_b(:,:,t) OIRE_opt_MSE(:,:,t) OIRE_opt_index(:,:,t)]=OIRE(y,x,iter);
end
調用的函數
function [b_OIRE OIRE_opt_b OIRE_opt_MSE OIRE_opt_index]=OIRE(y,x,iter)
dim=1;
pool=[10,100,1000,10000,1000000,10000000];
count=0;
[n, p]=size(x);
b=x\y;
b_OIRE = b; % [#1] initialize b_LRRE as b
sigma_sq=((y-x*b)'*(y-x*b))/(n-p); %'
b_act=[1;0;1;1;0;1;1];
econFlag=0;
[U,sigma,V] = svd(x,econFlag);
U1=U(:,1:p);
d=zeros(p,1);
d=diag(d);
alpha=V'*b_OIRE; %'
Delta=sigma.^1;
Delta=diag(Delta);
f=Delta.*alpha;
F=diag(f);
Theta=sum(f);
c=p^2*sigma_sq+p*Theta^2;
g=Theta*sum(alpha);
I=ones(p,1);
a=sigma_sq*I+Theta*f;
b=F*alpha;
k=zeros(p,1);
A=sigma_sq*eye(p)+F.^2;
varRho=(g-a'*pinv(A)*b)*pinv(c-a'*pinv(A)*a);
k=pinv(A)*b-varRho*pinv(A)*a;
K=diag(k);
D=varRho*I*I';
b_OIRE= V*(K+D)*U1'*y;
MSE=(k'*A*k)+(2*varRho*a'*k)-(2*b'*k)+(c*varRho^2)-(2*g*varRho)+(alpha'*alpha);
best_OIRE_MSE=MSE;
best_b_OIRE=b_OIRE;
for jj=1:iter % [## "iter" denotes the iteration number]
alpha=V'*b_OIRE; %'
Delta=sigma.^1; % [Error! not sigma.^2 but sigma.^1]
Delta=diag(Delta);
f=Delta.*alpha;
F=diag(f);
Theta=sum(f);
c=p^2*sigma_sq+p*Theta^2;
g=Theta*sum(alpha);
I=ones(p,1);
a=sigma_sq*I+Theta*f;
b=F*alpha;
k=zeros(p,1);
A=sigma_sq*eye(p)+F.^2;
varRho=(g-a'*pinv(A)*b)*pinv(c-a'*pinv(A)*a);
k=pinv(A)*b-varRho*pinv(A)*a;
K=diag(k);
D=varRho*I*I';
b_OIRE= V*(K+D)*U1'*y;
MSE=(k'*A*k)+(2*varRho*a'*k)-(2*b'*k)+(c*varRho^2)-(2*g*varRho)+(alpha'*alpha);
if(MSE<best_OIRE_MSE)
best_b_OIRE=b_OIRE;
best_OIRE_MSE=MSE;
best_index=jj+1;
end
if(any(jj == pool))
count=count+1;
OIRE_opt_b(:,count)=best_b_OIRE;
OIRE_opt_MSE(count)=best_OIRE_MSE;
OIRE_opt_index(count)=best_index;
end
end
end
matlabpool close
只需運行代碼即可。對我來說沒有錯誤。 – Marcin