parfor
Parallel for-loop
Syntax
Description
parfor executes a series of MATLAB® statements for values of loopVar = initVal:endVal; statements;
endloopvar between
initval and endval, inclusive,
which specify a vector of increasing integer values.
The loop runs in parallel when you have 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
increments loopVar = initVal:step:endVal; statements;endloopVar by the value of
step on each iteration, or decrements
loopVar when the value of step is
negative. step must be 1 or
-1. Use this syntax to run
parfor-loops with a descending loop index variable.
If you use this syntax, loop iterations must be consecutive integer values.
Before R2026a: step must be
1 and loop iterations must be consecutive, increasing
integer values.
parfor(loopVar = initVal:endVal); statements; end and
parfor(loopVar = initVal:step:endVal); statements; end also
execute statements in a loop. Use parenthesis around the loop expression to
allow for additional options.
parfor(___, uses M); statements;
endM to specify the maximum number of
workers or threads to use in evaluating statements in the loop body.
M must be a nonnegative integer.