How do you parallelize MATLAB code to print all possible 3-tuples more quickly?

Viewed 42

How do I parallelize the serial MATLAB code below? You can assume the printing of the 3-tuple in different order is fine, so long as all possible permutations are printed.

    for dt=1e-10:1e-10:1e-9
        for dx=1e-7:1e-7:1e-6
            for dy=1e-7:1e-7:1e-6
               disp(dx)
               disp(dy)
               disp(dt)
            end
        end
    end
2 Answers

here you go:

dt=1e-10:1e-10:1e-9;
dx=1e-7:1e-7:1e-6;
dy=1e-7:1e-7:1e-6;

[a1, a2, a3]=meshgrid(dx,dy,dt);
answer=[a1(:) a2(:) a3(:)]

I'm not sure what exactly you mean by "printing", the above code is a vectorized version of your for loops, where each row in answer is the 3 dx,dy,dt that the nested loop would spit out each iteration.

The other answer is the right answer. But, if you really wanted to parallelize your printing for whatever reason:

numberOfThreads = 4
parpool(numberOfThreads)
parfor dt=1e-10:1e-10:1e-9
    for dx=1e-7:1e-7:1e-6
        for dy=1e-7:1e-7:1e-6
           disp(dx)
           disp(dy)
           disp(dt)
        end
    end
end
Related