Re: Best code to match corresponding list items?
- To: mathgroup at smc.vnet.net
- Subject: [mg27756] Re: [mg27729] Best code to match corresponding list items?
- From: Jacqueline Zizi <jazi at club-internet.fr>
- Date: Wed, 14 Mar 2001 04:06:54 -0500 (EST)
- References: <200103130852.DAA26255@smc.vnet.net>
- Sender: owner-wri-mathgroup at wolfram.com
I hope that I understood well the question and below follows your demand and
an answer of your several questions:
1) MapThread [List, {A, B}] will do it, if the lists A and B are given.
2) And to start from scratch :
To create the lists A and B:
foo [a_, n_] := Table [ToExpression [ToString[a] <> ToString [i]], {i, 1, n}]
To do what you want "with a function" :
foofoo[n_] := With[{A = foo[a, n], B = foo[b, n]}, MapThread[List, {A, B}]]
Efficiency:
Timing [foofoo[100]] gives 0.0833333 Seconds
Timing [foofoo[1000]] gives 0.816667 Seconds
Timing [foofoo[1000]] gives 8.28333 Seconds
looks like linear time on my old Mac G3, 3 years old. For efficiency I have
no other idea to compare with at the moment. I'm looking forward other
solutions from the group to learn more about Mathematica and comparison
times.
3) Generalization: I don't know exactly what notations you want. I suggest
one solution that you might adapt . Else please tell us what you want
exactly as notations. This following function will create m lists of root a
of length n and do the job:
foofoofoo[a_, n_, m_] := MapThread[List, foo [#, n] & /@ foo [a, m]]
Examples:
------------
foofoofoo[a, 10, 2] creates 2 lists of length 10: {a11, a12, ?a110} et {a21,
a22, ?a210} and return:
{{a11, a21}, {a12, a22}, {a13, a23}, {a14, a24}, {a15, a25}, {a16, a26},
{a17,
a27}, {a18, a28}, {a19, a29}, {a110, a210}}
foofoofoo[a, 5, 7] creates 7 lists of length 5: {a11, a12, a13, a14,a15},
{a21, a22, a23, a24,a25}, ?
{a71, a72, a73, a74,a75} and return:
{{a11, a21, a31, a41, a51, a61, a71},
{a12, a22, a32, a42, a52, a62, a72},
{a13, a23, a33, a43, a53, a63, a73},
{a14, a24, a34, a44, a54, a64, a74},
{a15, a25, a35, a45, a55, a65, a75}}
foofoofoo[x, 9, 11]
{{x11, x21, x31, x41, x51, x61, x71, x81, x91, x101, x111},
{x12, x22, x32, x42, x52, x62, x72, x82, x92, x102, x112},
{x13, x23, x33, x43, x53, x63, x73, x83, x93, x103, x113},
{x14, x24, x34, x44, x54, x64, x74, x84, x94, x104, x114},
{x15, x25, x35, x45, x55, x65, x75, x85, x95, x105, x115},
{x16, x26, x36, x46, x56, x66, x76, x86, x96, x106, x116},
{x17, x27, x37, x47, x57, x67, x77, x87, x97, x107, x117},
{x18, x28, x38, x48, x58, x68, x78, x88, x98, x108, x118},
{x19, x29, x39, x49, x59, x69, x79, x89, x99, x109, x119}}
Hope this helps.
Jacqueline Zizi
Roger Ison wrote:
> Given two lists
> A={a1,a2,a3,...}
> B={b1,b2,b3...}
>
> I want to produce the list
> C={ {a1,b1}, {a2,b2}, {a3,b3}...} }
> as efficiently as possible.
> Better yet, generalize to do this with N lists all of same length.
>
> Obviously it can be done directly with Table, but is there a more elegant,
> implicit way to write it that doesn't involve passing a function to Table
> and using an explicit index? Is there a general functional primitive for
> this that I simply haven't found? Seems like something so common, there
> ought to be a built-in function for it.
>
> Thanks,
> Roger
- References:
- Best code to match corresponding list items?
- From: "Roger Ison" <rison@ix.netcom.com>
- Best code to match corresponding list items?