If the running time for your code is highly variable, then it is likely that you have a problem where sometimes, to some extent, you are verging into virtual memory. When MATLAB must use swap space, things slow down dramatically. After all, you are using disk space to substitute for the availability of physical RAM. This is the best reason I can offer for a code that varies in time by an order of magnitude in the time taken for different runs.
Use the profiler tool to determine where the time is going. Look carefully at those lines. Are you creating a large array, even if that is done temporarily in some function you call?
Check your system. Look to see if when this happens, if the CPU is running essentially idle while disk accesses are heavy.
Of course, if this is as I suggest, then the solution is logical too.
Make your algorithm more efficient in terms of memory. This is ALWAYS a good thing.
Get more RAM if you currently have less than 2GB.
Use the 64 bit version of MATLAB along with getting more RAM.
It is not really a MATLAB-related question (although on some earlier R14 versions antivirus could make it run slow). As a rule of thumb: to run fast you have to do less. – None – 2010-06-19T22:19:17.697