This is machine translation

Translated by Microsoft
Mouseover text to see original. Click the button below to return to the English version of the page.

Note: This page has been translated by MathWorks. Click here to see
To view all translated materials including this page, select Country from the country navigator on the bottom of this page.


Parallel for loop


parfor loopvar = initval:endval; statements; end
parfor (loopvar = initval:endval, M); statements; end


parfor loopvar = initval:endval; statements; end executes a series of MATLAB® statements for values of loopvar between initval and endval, inclusive, which specify a vector of increasing integer values. The loop runs in parallel when you have the Parallel Computing Toolbox™ or when you create a MEX function or standalone code with MATLAB Coder™. Unlike a traditional for-loop, iterations are not executed in a guaranteed order. You cannot call scripts directly in a parfor-loop. However, you can call functions that call scripts.

parfor (loopvar = initval:endval, M); statements; end executes statements in a loop using a maximum of M workers or threads, where M is a nonnegative integer.


Perform three large eigenvalue computations using three workers or cores with Parallel Computing Toolbox software:

parfor i=1:3, c(:,i) = eig(rand(1000)); end


  • If you have Parallel Computing Toolbox software, see the function reference pages for parfor and parpool for additional information.

  • If you have MATLAB Coder software, see the parfor function reference page for additional information.

Extended Capabilities

See Also

Introduced in R2008a