By default mex file cannot be executed in parallel in Matlab, which will be a major bottleneck for Matlab program. Fortunately, there is a way to execute loop operations in parallel -- parfor. Matlab provides a good tutorial on parfor: Getting Started with parfor.

One tricky problem with parfor is dealing with reduction assignments, which means the value of some variables are updated by each iteration, such as assignment to an array/matrix indexed by loop index.  My suggestion is to separate the loop into two parts: one part deal with non-reduction assignment with parfor, and the other part do the reduction assignment with traditional for loop.

If you cannot save all the parfor-assigned variables in memory, then you must save them into mat file and load them in the next for-loop. In the parfor-loop, command "save" cannot  be used directly because it violates the transparency(God knows what does it mean!). As an alternative, you can create a wrapper function of save and call that function instead. FOr example:

Save the following as "parsave.m":
function parsave(fname, x,y)
save(fname, 'x', 'y')

Then run it with:
parfor ii = 1:4
x = rand(10,10);
y = ones(1,3);
parsave(sprintf('output%d.mat', ii), x, y);


  1. parfor manual:
  2. Getting Started with parfor:
  3. How do I use SAVE with a PARFOR loop: