1
$\begingroup$

I'm very new to this. My goal is to run a function for 10 different pairs of values. The function looks like this:

f[groupsize_, solvedforq_] = 
Sum[Binomial[groupsize, t] Chop[solvedforq]^t (1 - Chop[solvedforq])^(groupsize - t), {t, 3, groupsize}]

The objects groupsize and solvedforq are lists, each containing 10 values. I would like to create a List or Table of outputs, where the first output is the solution for taking the first element of each list (i.e., the pair of values that I get when taking the first element of groupsize and the first element of solvedforq), the second output is the solution for taking the second element of each list, etc...

So I will ideally have a List or Table with 10 output values. I am ware for the command Map[], but I wouldn't know how to use it in this more complex case. Thanks for all help!

$\endgroup$

2 Answers 2

1
$\begingroup$

One way is with MapThread:

MapThread[f, {group, solved}]

This will give f[group[[1]], solved[[1]]], f[group[[2]], solved[[2]]], etc. Note that your function f should probably be defined with := instead of =.

Bob Hanlon suggest the use of Transpose, and this brings up a good point. There is a tight relationship between here: Thread is a synonym for Transpose (at least for 2D matix-style lists), so this could also be written:

Map[f, Thread[{group, solved}]]

or using the shortcut

f /@ Thread[{group, solved}]

Here it is easy to see the relationship between Map and Thread, and MapThread. This also demonstrates how similar the functioning is between Map /@ and Apply @@@:

f @@@ Thread[{group, solved}]

or the more pedantic

Apply[f, Thread[{group, solved}], 2]

which also give the same output.

$\endgroup$
2
  • $\begingroup$ Works perfectly, thank you! $\endgroup$
    – HP Bax
    Nov 13, 2018 at 14:55
  • 2
    $\begingroup$ Also f @@@ Transpose@{group, solved} $\endgroup$
    – Bob Hanlon
    Nov 13, 2018 at 14:57
1
$\begingroup$

Here is another variation, with Table.

Table[f[group[[i]], solved[[i]]], {i, 1, 4}]

I assume

group = {1, 2, 3, 4};
solved = {5, 6, 7, 8};
f[groupsize_, solvedforq_] := etc

For these sets, the output is the same as in the previous case. {0, 0, 343, -10240}

$\endgroup$

Your Answer

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

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