Main Content

parfor

Parallel for loop

Syntax

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

Description

parfor loopvar = initval:endval;statements; endexecutes a series of MATLAB®statements for values ofloopvarbetweeninitvalandendval, 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 withMATLAB Coder™. Unlike a traditionalfor-loop, iterations are not executed in a guaranteed order. You cannot call scripts directly in aparfor-loop. However, you can call functions that call scripts.

parfor (loopvar = initval:endval, M);statements; endexecutes statements in a loop using a maximum ofMworkers or threads, whereMis a nonnegative integer.

Examples

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

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

Tips

  • If you have Parallel Computing Toolbox software, see the function reference pages forparfor(Parallel Computing Toolbox)andparpool(Parallel Computing Toolbox)for additional information.

  • If you haveMATLAB Codersoftware, see theparfor(MATLAB Coder)function reference page for additional information.

Extended Capabilities

See Also

Introduced in R2008a