请教如何在使用matlab GPU时记录arrayfun中的循环迭代变量?

在下述代码中,我希望能记录arrayfun函数里面的循环变量z到一个矩阵中,以作进一步的操作。Matlab版本为2017b,代码运行报错为“Array indexing is not supported”。请各位大侠赐教如何才能解决这一问题。

clear all
clc
maxIterations=100;
gridSize=300;
xlim=[-0.75, -0.73];
ylim=[ 0.12,  0.14];
t=tic();
x=gpuArray.linspace(xlim(1), xlim(2), gridSize);
y=gpuArray.linspace(ylim(1), ylim(2), gridSize);
[xGrid,yGrid]=meshgrid(x,y);
Pos=gpuArray.zeros(maxIterations,1);
count=parent_fun(xGrid,yGrid,maxIterations,Pos);
count=gather(count);
gpuArrayfunTime=toc(t)
figure(1)
imagesc(x,y,count)
reset(gpuDevice(1))

%下面是用到的子函数。使用nested function的形式是希望能进行索引,但无效。
function result=parent_fun(xGrid,yGrid,maxIterations,Pos)
             function count=tar_fun(x0,y0)
                           z0=complex(x0,y0);
                           z=z0;
                           count=1;
                           while (count<=maxIterations) && (abs(z)<=2)
                                   count=count+1;
                                   z=z*z+z0;
                                   Pos(count,1)=z;     % 希望对每次迭代的z值进行记录,但这里报错不支持索引。
                            end
                            count=max(log(count),log(abs(z)));
              end
              result=arrayfun(@tar_fun, xGrid, yGrid);
end

猜你喜欢

转载自blog.csdn.net/weixin_44120272/article/details/85034976