10
$\begingroup$

I am interested in finding the most concise elegant way of doing the following: We have a function fun[x1,x2] and two lists:

ls1={a1,a2,a3,a4,......} 

and

ls2={b1,b2,b3,b4,......}

I want my result to be:

 Rslt={{{a1,fun[a1,b1]},{a1,fun[a1,b2]},{a1,fun[a1,b3]},.......},
        {{a2,fun[a2,b1]},{a2,fun[a2,b2]},{a2,fun[a2,b3]},.......}, 
        {{a3,fun[a3,b1]},{a3,fun[a3,b2]},{a3,fun[a3,b3]},.......},......}

Thanks for help.

$\endgroup$
1
  • 1
    $\begingroup$ Table[{i, func[i, j]}, {i, ls1}, {j, ls2}] $\endgroup$
    – matrix42
    Commented Nov 16, 2017 at 5:08

1 Answer 1

15
$\begingroup$

Well, that's an easy one to write and maybe a bit harder to understand:

ls1 = {a1, a2, a3, a4};
ls2 = {b1, b2, b3, b4};

Outer[{#1, func[##]} &, ls1, ls2]

Mathematica graphics

$\endgroup$
3
  • $\begingroup$ this works as well Outer[{#, func[##]} &, ls1, ls2] $\endgroup$ Commented Nov 16, 2017 at 12:40
  • $\begingroup$ @José I wonder what you wanted to say by that. $\endgroup$
    – LLlAMnYP
    Commented Nov 20, 2017 at 15:09
  • $\begingroup$ Oh, no need of adding ‘1’ to ‘#’, nothing special. $\endgroup$ Commented Nov 20, 2017 at 16:02

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.