MATLAB Answers


Parfor Step

Asked by Brian
on 11 Jun 2012

I am attempting to run a ParFor statement with a step function and Matlab is giving me an error "The range of a parfor statement must be increasing consecutive integers. See Parallel Computing Toolbox, "parfor"." I'm wondering if this is possible or if I have to do some manipulation to my loop to get this to work in parallel. Would someone be able to make a suggestion as to how to modify one of these loops so that it will work in parallel?

Simplified Example

      parfor (i = 1:10:75,8)
          test3(i)= i*3;

Real Example

parfor (i = 1:10:75,8)
    recnum = min(i+9,length(test));

Thanks a lot! Brian


Sorry? What do you intend

for (i = 1:10:75,8)

to mean? Is it intended as

for i = [1:10:75, 8]


for i = [1 11 21 31 41 51 61 71 8]


on 11 Jun 2012

Yeah, sorry for being unclear. In my real example I am breaking up the size of my datainsert command by using a step function. I'm doing this because I'm inserting about 28 million records and the java heap size in Matlab is running out of memory. SInce I'm breaking it up, I'd like to do the datainsert in parallel so that it's not so slow.

1 Answer

Answer by Edric Ellis
on 12 Jun 2012
 Accepted answer

As the doc states, PARFOR needs to work with unit-step ranges. So you do need to re-work the loop slightly, something like this:

   idx = 1:10:75;
   parfor (i = 1:numel(idx), 8)
     val = idx(i);
     test3t(i) = val * 3;

That example leaves 'test3t' slightly different to 'test3' in your example, you can rearrange things by doing

test3(idx) = test3t;


on 12 Jun 2012

Thanks a lot Edric. I am still new to Matlab and frankly, sometimes the documentation, although very thorough, goes right over my head.


Edric Ellis
on 12 Jun 2012

By the way, the syntax using the extra parentheses around the PARFOR arguments is rarely needed, it's much simpler just to write "parfor i = 1:numel(idx)".

Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

MATLAB Academy

New to MATLAB?

Learn MATLAB today!