How to automatically choose to compute for or parfor loops

5 views (last 30 days)
I wonder if there's a way to specify if I want to compute with a normal for or with a parfor.
What I don't want is to make a switch with two cases (case 1 --> parfor; case2 --> for;) because what's inside the loop is quite long so I don't want to copy/paste so many code lines and it's not 100% necessary to make this choice automatic. But sometimes I have to calculate few inputs and sometimes a lot of them.
EDIT: I guess I can make two functions: 1 for looped and another parfor looped, but is there any other option?

Accepted Answer

Sean de Wolski
Sean de Wolski on 21 Aug 2015
You can use this syntax for parfor
parfor (loopvar = initval:endval, M); statements; end
Where M is dynamically determined. If it's zero, then this loop will run serially.

Sign in to comment.

More Answers (0)


Find more on Parallel Computing Fundamentals in Help Center and File Exchange


Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!