| Products & Services | Solutions | Academia | Support | User Community | Company |
| Download Product Updates | | | Get Pricing | | | Trial Software |
| Documentation → MATLAB |
| Contents | Index |
| Learn more about MATLAB |
parfor loopvar = initval:endval; statements; end
parfor (loopvar = initval:endval, M); statements;
end
parfor loopvar = initval:endval; statements; end executes a series of MATLAB commands denoted here as statements for values of loopvar between initval and endval, inclusive, which specify a vector of increasing integer values. Unlike a traditional for-loop, there is no guarantee of the order in which the loop iterations are executed.
The general format of a parfor statement is:
parfor loopvar = initval:endval
<statements>
end
Certain restrictions apply to the statements to ensure that the iterations are independent, so that they can execute in parallel. If you have the Parallel Computing Toolbox™ software, the iterations of statements can execute in parallel on separate MATLAB workers on your multi-core computer or computer cluster.
To execute the loop body in parallel, you must open a pool of MATLAB workers using the matlabpool function, which is available in Parallel Computing Toolbox.
parfor (loopvar = initval:endval, M); statements; end executes statements in a loop using a maximum of M MATLAB workers to evaluate statements in the body of the parfor-loop. Input variable M must be a nonnegative integer. By default, MATLAB uses up to as many workers as it finds available.
When any of the following are true, MATLAB does not execute the loop in parallel:
There are no workers in a MATLAB pool
You set M to zero
You do not have Parallel Computing Toolbox
If you have Parallel Computing Toolbox, you can read more about parfor and matlabpool by typing
doc distcomp/parfor doc distcomp/matlabpool
Perform three large eigenvalue computations using three computers or cores:
matlabpool(3) parfor i=1:3, c(:,i) = eig(rand(1000)); end
![]() | pareto | parse (inputParser) | ![]() |

Includes the most popular MATLAB recorded presentations with Q&A sessions led by MATLAB experts.
| © 1984-2009- The MathWorks, Inc. - Site Help - Patents - Trademarks - Privacy Policy - Preventing Piracy - RSS |