In the following attached code, I would like to record the variable *Z* in the while loop as array for further process, for example, finding the max value of Z in the array. But *array indexing is not supported* in Matlab 2017b, even the nested function used. Can someone provide me with details to deal this question?

```
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))
```

The nested function is defined as follows.

```
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; % where error arises!
end
count=max(log(count),log(abs(z)));
end
result=arrayfun(@tar_fun, xGrid, yGrid);
end
```