Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.soft-sys.math.mathematica > #5652 > unrolled thread
| Started by | Szymon Roziewski <szymon.roziewski@gmail.com> |
|---|---|
| First post | 2011-10-15 10:08 +0000 |
| Last post | 2011-10-16 11:17 +0000 |
| Articles | 5 — 5 participants |
Back to article view | Back to comp.soft-sys.math.mathematica
How to create this kind of list Szymon Roziewski <szymon.roziewski@gmail.com> - 2011-10-15 10:08 +0000
Re: How to create this kind of list Helen Read <readhpr@gmail.com> - 2011-10-16 11:08 +0000
Re: How to create this kind of list "Oleksandr Rasputinov" <oleksandr_rasputinov@hmamail.com> - 2011-10-16 11:11 +0000
Re: How to create this kind of list "Dr. Wolfgang Hintze" <weh@snafu.de> - 2011-10-16 11:16 +0000
Re: How to create this kind of list dimitris <dimmechan@yahoo.com> - 2011-10-16 11:17 +0000
| From | Szymon Roziewski <szymon.roziewski@gmail.com> |
|---|---|
| Date | 2011-10-15 10:08 +0000 |
| Subject | How to create this kind of list |
| Message-ID | <j7bm30$35r$1@smc.vnet.net> |
Hello there,
I am thinking about creating such a list.
I have 2 lists with the same number of elements.
The idea is to create one list which was taken from these previous two ones
in the following way:
Take one element of the first list and one from another and put it into
result list {{x1,y1},{x2,y2},...,{xn,yn}}.
Input lists:
A={x1,x2,...,xn}
B={y1,y2,...,yn}
All best,
Szymon Roziewski
[toc] | [next] | [standalone]
| From | Helen Read <readhpr@gmail.com> |
|---|---|
| Date | 2011-10-16 11:08 +0000 |
| Message-ID | <j7edvh$ehc$1@smc.vnet.net> |
| In reply to | #5652 |
On 10/15/2011 6:08 AM, Szymon Roziewski wrote:
> Hello there,
> I am thinking about creating such a list.
> I have 2 lists with the same number of elements.
> The idea is to create one list which was taken from these previous two ones
> in the following way:
> Take one element of the first list and one from another and put it into
> result list {{x1,y1},{x2,y2},...,{xn,yn}}.
> Input lists:
> A={x1,x2,...,xn}
> B={y1,y2,...,yn}
>
> All best,
> Szymon Roziewski
Here are two ways. (And BTW, it helps when you post here to include an
example that others can copy/paste into Mathematica instead of having
make up an example for you.) Anyway, let's make a couple of lists:
list1 = RandomInteger[10, 5]
list2 = RandomInteger[{15, 20}, 5]
One method: Use Riffle to make a new list alternating elements from
list1 and list2, then Partition into pairs, like so.
Partition[Riffle[list1, list2], 2]
Second method: Put list1 and list2 into matrix with list1 as the first
row and list2 as the second row, then Transpose the matrix.
Transpose[{list1, list2}]
--
Helen Read
University of Vermont
[toc] | [prev] | [next] | [standalone]
| From | "Oleksandr Rasputinov" <oleksandr_rasputinov@hmamail.com> |
|---|---|
| Date | 2011-10-16 11:11 +0000 |
| Message-ID | <j7ee4a$ein$1@smc.vnet.net> |
| In reply to | #5652 |
On Sat, 15 Oct 2011 11:08:32 +0100, Szymon Roziewski
<szymon.roziewski@gmail.com> wrote:
> Hello there,
> I am thinking about creating such a list.
> I have 2 lists with the same number of elements.
> The idea is to create one list which was taken from these previous two
> ones
> in the following way:
> Take one element of the first list and one from another and put it into
> result list {{x1,y1},{x2,y2},...,{xn,yn}}.
> Input lists:
> A={x1,x2,...,xn}
> B={y1,y2,...,yn}
>
> All best,
> Szymon Roziewski
a = Array[x, 5];
b = Array[y, 5];
lst = Transpose[{a,b}]
gives
{{x[1], y[1]}, {x[2], y[2]}, {x[3], y[3]}, {x[4], y[4]}, {x[5], y[5]}}
[toc] | [prev] | [next] | [standalone]
| From | "Dr. Wolfgang Hintze" <weh@snafu.de> |
|---|---|
| Date | 2011-10-16 11:16 +0000 |
| Message-ID | <j7eees$ekp$1@smc.vnet.net> |
| In reply to | #5652 |
You can use Transpose[] like this
n = 3;
va = Table[a[i], {i, 1, n}]
vb = Table[b[i], {i, 1, n}]
{a[1], a[2], a[3]}
{b[1], b[2], b[3]}
Transpose[{va, vb}]
{{a[1], b[1]}, {a[2], b[2]}, {a[3], b[3]}}
--- Wolfgang
"Szymon Roziewski" <szymon.roziewski@gmail.com> schrieb im Newsbeitrag
news:j7bm30$35r$1@smc.vnet.net...
> Hello there,
> I am thinking about creating such a list.
> I have 2 lists with the same number of elements.
> The idea is to create one list which was taken from these previous
> two ones
> in the following way:
> Take one element of the first list and one from another and put it
> into
> result list {{x1,y1},{x2,y2},...,{xn,yn}}.
> Input lists:
> A={x1,x2,...,xn}
> B={y1,y2,...,yn}
>
> All best,
> Szymon Roziewski
[toc] | [prev] | [next] | [standalone]
| From | dimitris <dimmechan@yahoo.com> |
|---|---|
| Date | 2011-10-16 11:17 +0000 |
| Message-ID | <j7eefq$el5$1@smc.vnet.net> |
| In reply to | #5652 |
There are several ways to do this. You should check what is the more
efficient!
Say the 2 lists are the following:
lst1 = Table[Random[Integer, {-100, 100}], {10}]
{-14, -62, 2, 63, -9, 78, -76, 1, -98, 26}
lst2 = Table[Random[Integer, {-100, 100}], {10}]
{-60, -98, 31, 66, -97, 47, 18, -40, 94, -76}
Then one way to proceed is:
({lst1[[#1]], lst2[[#1]]} & ) /@ Range[Length[lst1]]
{{-14, -60}, {-62, -98}, {2, 31}, {63, 66}, {-9, -97}, {78, 47}, {-76,
18}, {1, -40}, {-98, 94}, {26, -76}}
Another way is the following:
Transpose[{lst1, lst2}]
{{-14, -60}, {-62, -98}, {2, 31}, {63, 66}, {-9, -97}, {78, 47}, {-76,
18}, {1, -40}, {-98, 94}, {26, -76}}
A third way is:
Thread[{lst1, lst2}]
{{-14, -60}, {-62, -98}, {2, 31}, {63, 66}, {-9, -97}, {78, 47}, {-76,
18}, {1, -40}, {-98, 94}, {26, -76}}
Similarly,
MapThread[List, {lst1, lst2}]
{{-14, -60}, {-62, -98}, {2, 31}, {63, 66}, {-9, -97}, {78, 47}, {-76,
18}, {1, -40}, {-98, 94}, {26, -76}}
Best Regards
Dimitris
[toc] | [prev] | [standalone]
Back to top | Article view | comp.soft-sys.math.mathematica
csiph-web