Could anyone help me to access multiple elements of an array (xGrid and yGrid here) from inside a function (fun.m). Performance is my critical point. Here is the function (that is called):
function count = fun(x0,y0,maxIter)
z0 = complex(x0,y0);
z = z0;
count = 1;
while count <= maxIter && (real(z)*real(z) + imag(z)*imag(z)) <= 4
count = count + 1;
z = z*z + z0;
end
count = log(count);
And the Main:
x = parallel.gpu.GPUArray.linspace( xlim(1), xlim(2), gridSize );
y = parallel.gpu.GPUArray.linspace( ylim(1), ylim(2), gridSize );
[xGrid,yGrid] = meshgrid( x, y );
count = arrayfun(@fun,xGrid, yGrid, maxIter);
I know I only get access to x0 and y0 (inside fun/arrayfun, when i=1, j=1), but can I also access x1, x2… and y1, y2.. at that same position (i=1 and j=1) and without passing them in parameters?
(sorry for the earlier answer, I misread the question)
If you want to access other elements of xGrid and yGrid from inside fun, why not pass xGrid, yGrid into the function? Matlab uses copy-on-write, so passing them in shouldn’t impact the efficiency of the code unless you write to them.