我在Matlab中遇到parfor
的问题。我已阅读此http://uk.mathworks.com/help/distcomp/parallel-for-loops-parfor.html但我无法理解如何修复我的代码。我认为函数Inv
的递归性质存在问题。你能帮帮我吗?
这是代码的简化版本
R=10;
max_rec=10^3;
n=20;
set(0,'RecursionLimit',max_rec+4); %set the maximum number of recursions
Res=zeros(R,1);
parfor s=1:R
XY = unifrnd(0,1,n,2);
Z=zeros(n,1);
for p=1:n
x1=XY(p,1);
x2=XY(p,2);
Z(p)=Inv(x1, x2, max_rec);
end
Res(s)=sum(Z,1);
end
function t=Inv(x1,x2,iter)
t = 0;
if iter > 0
if x1<0.5
if x2<0.5
t=(0+Inv(2*x2, 2*x1, iter-1))/4;
else
t=(1+Inv(2*x1, 2*x2-1, iter-1))/4;
end
else
if x2>=0.5
t=(2+Inv(2*x1-1, 2*x2-1, iter-1))/4;
else
t=(3+Inv(1-2*x2, 2-2*x1, iter-1))/4;
end
end
end
end