4
$\begingroup$

I often need to explore a large parameter space, e.g. making dozens of plots using a range of parameters. This looks something like:

explore[a, b, c, d]

a = {1, 3, 6, 8, 9}
b = {"a", "b", "c"}
c = {2, 6}
d = {"X", "I", "k", "l"}

I then usually use n nested loops (4 in this case) to explore all combinations of parameters, and in each loop export a plot. I would suppose there is a more efficient, or at least a simpler way to code this. I tried Map, but would need more than one slot, and also Table, but could not fully reproduce exploring the entire parameter set.

$\endgroup$
4
  • 2
    $\begingroup$ Do you mean something like explore @@@ Tuples[{a, b, c, d}]? $\endgroup$ Commented Jun 23, 2017 at 11:51
  • $\begingroup$ That is not working. explore is a self written command, something like explore[a_, b_, c_, d_] := Module[{},], where a, b, c, d are parameters that are used in a longer and complex subprogram stored in module and executed using the explore command. $\endgroup$ Commented Jun 23, 2017 at 12:01
  • $\begingroup$ That should give you a list of all the outputs obtained by feeding all possible combinations of those parameters into your explore function, whatever the specifics of that function might be. Do the parameters come from a discrete set, as in your example? $\endgroup$ Commented Jun 23, 2017 at 12:20
  • $\begingroup$ That is beautiful! Working now. I just had to make some slight adjustments to values appearing only once, i.e. putting them into brackets. Thanks! $\endgroup$ Commented Jun 23, 2017 at 13:40

1 Answer 1

5
$\begingroup$

Tuples and Subsets and their associates are really handy when you're working with discrete sets. In your case:

a = {1, 3, 6, 8, 9};
b = {"a", "b", "c"};
c = {2, 6};
d = {"X", "I", "k", "l"};
explore @@@ Tuples[{a, b, c, d}]

(* Out= {explore[1, "a", 2, "X"], explore[1, "a", 2, "I"],... , explore[9, "c", 6, "l"]} *)

Depending on your explore function (i.e., if it takes a while and isn't already parallelized) and your actual parameters, you might also be able to speed things up with

Parallelize[explore @@@ Tuples[{a, b, c, d}], Method -> "FinestGrained"]

But probably not worth it if explore is fast already due to the overheads.

$\endgroup$
1
  • $\begingroup$ Thanks – it is worth to parallelise, and I wouldn't have done it without your hint. $\endgroup$ Commented Jun 23, 2017 at 18:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.