5
$\begingroup$

Given a function f and two lists of variables

{a1,a2,...}, {b1,b2,...}

How can I generate a following list

{f[a1,b1],f[a1,b2],...,f[a2,b1],f[a2,b2]...,}

==========================================

Thanks for the answering of @Bill first. I am not familiar with the function "Mapthread". The best thing I can do is as following

Mapthread[f,{l1,l2}]

leading to a list

{f[a1,b1],f[a2,b2],...}

There is no f[a1,b2] in this result. How can I get a full list that I want?

$\endgroup$
10
  • $\begingroup$ reference.wolfram.com/language/ref/MapThread.html $\endgroup$
    – Bill
    Commented Aug 30, 2021 at 5:53
  • $\begingroup$ I have considered the mapthread, but I didn't manage it. Could u show me the more details? $\endgroup$
    – Rui Yu
    Commented Aug 30, 2021 at 6:18
  • 1
    $\begingroup$ Frankly, the MapThread documentation contains everything anyone could write in an answer here. Voting to close as "simple to find in the documentation". $\endgroup$
    – Szabolcs
    Commented Aug 30, 2021 at 6:33
  • 1
    $\begingroup$ a = {a1, a2, a3}; b = {b1, b2, b3}; MapThread[f, {a, b}] gives {f[a1, b1], f[a2, b2], f[a3, b3]} and then EDIT: the question got changed. $\endgroup$
    – Syed
    Commented Aug 30, 2021 at 6:39
  • 1
    $\begingroup$ Thanks for the edit. Now it's clearer. Voted to reopen. $\endgroup$
    – Szabolcs
    Commented Aug 30, 2021 at 18:12

3 Answers 3

5
$\begingroup$

Originally you were asking:

a = {a1, a2, a3};
b = {b1, b2, b3};
MapThread[f, {a, b}]

{f[a1, b1], f[a2, b2], f[a3, b3]}

How can I get a full list that I want?

What you are now looking for is called an outer product.

Flatten[Outer[List, a, b], 1]
{{a1, b1}, {a1, b2}, {a1, b3}, {a2, b1}, {a2, b2}, {a2, b3}, {a3, 
  b1}, {a3, b2}, {a3, b3}}
f @@@ Flatten[Outer[List, a, b], 1]
{f[a1, b1], f[a1, b2], f[a1, b3], f[a2, b1], f[a2, b2], f[a2, b3], 
 f[a3, b1], f[a3, b2], f[a3, b3]}
$\endgroup$
6
  • $\begingroup$ Thank you very much! This is just the problem at beginning. $\endgroup$
    – Rui Yu
    Commented Aug 30, 2021 at 6:49
  • 2
    $\begingroup$ @RuiYu This is not the proper way to ask a question. You cannot change your question every 2 minutes. Please return to the forum when you have formed the question. $\endgroup$
    – Syed
    Commented Aug 30, 2021 at 6:51
  • $\begingroup$ Thanks. I did not change my question. Maybe at beginning it is a little misleading. I will be more cautious next time. By the way, how to close a question? $\endgroup$
    – Rui Yu
    Commented Aug 30, 2021 at 6:55
  • 2
    $\begingroup$ Users with "Close" vote privileges can close your questions. It's okay, no one minds helping you out, but it is in your interest to ask well-formed stable and non-trivial questions, otherwise responders will have lesser motivation to answer them. $\endgroup$
    – Syed
    Commented Aug 30, 2021 at 7:01
  • 6
    $\begingroup$ Flatten@Outer[f, a, b] $\endgroup$
    – LouisB
    Commented Aug 30, 2021 at 7:12
6
$\begingroup$
a = {a1, a2, a3};
b = {b1, b2, b3};

f @@@ Tuples[{a, b}]
(*    {f[a1, b1], f[a1, b2], f[a1, b3],
       f[a2, b1], f[a2, b2], f[a2, b3],
       f[a3, b1], f[a3, b2], f[a3, b3]}    *)
$\endgroup$
6
$\begingroup$
a = {a1, a2, a3}; b = {b1, b2, b3};

Tuples[f[a, b]]
{f[a1, b1], f[a1, b2], f[a1, b3], 
 f[a2, b1], f[a2, b2], f[a2, b3], 
 f[a3, b1], f[a3, b2], f[a3, b3]}

Also

Distribute[f[a, b], List]
{f[a1, b1], f[a1, b2], f[a1, b3], 
 f[a2, b1], f[a2, b2], f[a2, b3], 
 f[a3, b1], f[a3, b2], f[a3, b3]}
$\endgroup$
1
  • 1
    $\begingroup$ These suggestions won't work if f aggressively executes right away and f[a, b] generates a result (instead of staying symbolic). Safer to use my solution, I think. $\endgroup$
    – Roman
    Commented Jun 19 at 11:35

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.