1

Currently I am writing a MATLAB program, which generates a fractal, based off of user-defined inputs. The problem is that when I run this piece of code with large arrays it takes an enormous amount of time to process. I am looking for advice or suggestions on how to optimize this code. Any responses will be greatly appreciated.
The main program is

for j=1:1:rows
    for k=1:1:rows
        Z(j,k)=iter(Z(j,k),c1(j,k),niter,f1);%Z and c1 are complex number,niter is an integer f1 is a handle
    end
end

The function iter is

function z=iter(z1,c,niter,f1)

for i=1:1:niter
    z1=f1(z1,c);
    if abs(z1)<=2
        z=i;
    else
        z=i;
        break;
    end
end
end
Autonomous
  • 8,935
  • 1
  • 38
  • 77
  • 1
    To reduce computation time you should try to vectorize, that is, get rid of the two "spatial" `for` loops. Take a look at [my answer to this question](http://stackoverflow.com/q/20441659/2586922). Even if that's not the same fractal as yours, you could use a similar approach: basically, work with the whole grid of points, all at the same time, keeping track of which points have not surpassed the threshold yet – Luis Mendo Feb 27 '14 at 00:11
  • 1
    Since the associated function isn't a big one, you can surely bring that into the calling function, i.e. do function inlining. Other suggestions - use pre-allocation and avoid ‘i' as the iterator (not a good practice with MATLAB programming). – Divakar Feb 27 '14 at 00:24
  • Yes, if you want help on vectorizing this, you'll need to provide us the `f1` handle... – reverse_engineer Feb 27 '14 at 14:21

0 Answers0