2
$\begingroup$

I have the following routine where I build a pair of function name lists {Flist, Klist} and 1D argument list {a1,a2,a3,a4,a5}:

T = Transpose@{ToExpression@ Table[StringJoin["a", ToString[i]], {i, 1, 5}]};
Flist = {F1, F2, F3};
Klist = {K1, K2, K3};
Flatten[Outer[{#1, #2} &, {Flist, Klist}, T], 3]

Out[149]= {{F1, a1}, {F1, a2}, {F1, a3}, {F1, a4}, {F1, a5},
{F2, a1}, {F2, a2}, {F2, a3}, {F2, a4}, {F2, a5},
{F3, a1}, {F3, a2}, {F3, a3}, {F3, a4}, {F3, a5},
{K1, a1}, {K1, a2}, {K1, a3}, {K1, a4}, {K1, a5},
{K2, a1}, {K2, a2}, {K2, a3}, {K2, a4}, {K2, a5},
{K3, a1}, {K3, a2}, {K3, a3}, {K3, a4}, {K3, a5}}

With the one argument or many, e.g. {Fn, arg1_n, arg2_n}, it works, but I am confused with the Outer function applied to the nested lists as to:

T = Transpose@{ToExpression@Table[StringJoin["a", ToString[i]], {i, 1, 5}],ToExpression@Table[StringJoin["b", ToString[i]], {i, 1, 5}]}

Out[151]= {{a1, b1}, {a2, b2}, {a3, b3}, {a4, b4}, {a5, b5}}

Flatten[Outer[{#1, #2} &, {Flist, Klist}, T, 1], 1]

Out[182]= {{{F1, F2, F3, F4, F5}, {a1, b1}}, {{F1, F2, F3, F4, 
   F5}, {a2, b2}}, {{F1, F2, F3, F4, F5}, {a3, b3}}, {{F1, F2, F3, F4,
    F5}, {a4, b4}}, {{F1, F2, F3, F4, F5}, {a5, b5}}, {{K1, K2, K3, 
   K4, K5}, {a1, b1}}, {{K1, K2, K3, K4, K5}, {a2, b2}}, {{K1, K2, K3,
    K4, K5}, {a3, b3}}, {{K1, K2, K3, K4, K5}, {a4, b4}}, {{K1, K2, 
   K3, K4, K5}, {a5, b5}}}

I played with dimensions allowed in Outer and Flatten and did not succeeded to obtain result in the following form:

{{F1, {a1, b1}}, {F1, {a2,b2}}, {F1, {a3,b3}},.......
{K3, {a3, b3}}, {K3, {a4, a4}}, {K3, {a5, b5}}}

Can someone advise how to obtained desired output having {Flist, Klist} and nested list T?

UPD Fr and Kr which @lericr asked below, was a typo in my code, which I fixed and recall them Klist and Klist. Sorry for omission.

$\endgroup$
4
  • $\begingroup$ What are Fr and Kr? $\endgroup$
    – lericr
    Commented May 10 at 22:59
  • $\begingroup$ Looks like Fr must be {F1, F2, F3, F4, F5} and similarly for Kr. $\endgroup$
    – lericr
    Commented May 10 at 23:01
  • 2
    $\begingroup$ Are wanting this? Flatten[Outer[List, Join[Fr, Kr], T, 1], 1] $\endgroup$
    – lericr
    Commented May 10 at 23:03
  • $\begingroup$ Thank you, works perfectly ! As well as a @ubpdqn's solution. Sorry for wrong names yesterday Fr' and 'Kr' are supposed to be Flist` and Klist` I did a mistake in the code. $\endgroup$
    – seva011
    Commented May 11 at 16:46

1 Answer 1

4
$\begingroup$

Tuples may be easier than Outer. For example, using:

T = Transpose@{ToExpression@
     Table[StringJoin["a", ToString[i]], {i, 1, 5}], 
    ToExpression@Table[StringJoin["b", ToString[i]], {i, 1, 5}]};
Flist = {F1, F2, F3};
Klist = {K1, K2, K3};

you can do:

Tuples[{Flist~Join~Klist, T}]

which yields:

{{F1, {a1, b1}}, {F1, {a2, b2}}, {F1, {a3, b3}}, {F1, {a4, 
   b4}}, {F1, {a5, b5}}, {F2, {a1, b1}}, {F2, {a2, b2}}, {F2, {a3, 
   b3}}, {F2, {a4, b4}}, {F2, {a5, b5}}, {F3, {a1, b1}}, {F3, {a2, 
   b2}}, {F3, {a3, b3}}, {F3, {a4, b4}}, {F3, {a5, b5}}, {K1, {a1, 
   b1}}, {K1, {a2, b2}}, {K1, {a3, b3}}, {K1, {a4, b4}}, {K1, {a5, 
   b5}}, {K2, {a1, b1}}, {K2, {a2, b2}}, {K2, {a3, b3}}, {K2, {a4, 
   b4}}, {K2, {a5, b5}}, {K3, {a1, b1}}, {K3, {a2, b2}}, {K3, {a3, 
   b3}}, {K3, {a4, b4}}, {K3, {a5, b5}}}
$\endgroup$

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.