Multiply two lists
Clash Royale CLAN TAG#URR8PPP
up vote
6
down vote
favorite
I have two lists, e.g.
a = 1,2,3
b = 4,5,6
I'd like to multiply each element from the first list with each element from the second, like this:
1*4, 2*4, 3*4, 1*5, 2*5, 3*5, 1*6, 2*6, 3*6
I only need the result:
4, 5, 6, 8, 10, 12, 15, 18
I have found a solution for this:
Drop[Flatten[Reap[For[i = 1, i < 4, i++, Sow[i*4, 5, 6]]]], 1]
However, I think this is overly complicated -- and number 12 is in there two times. Isn't there a much simpler way to do this? Multiply two lists?
list-manipulation
add a comment |Â
up vote
6
down vote
favorite
I have two lists, e.g.
a = 1,2,3
b = 4,5,6
I'd like to multiply each element from the first list with each element from the second, like this:
1*4, 2*4, 3*4, 1*5, 2*5, 3*5, 1*6, 2*6, 3*6
I only need the result:
4, 5, 6, 8, 10, 12, 15, 18
I have found a solution for this:
Drop[Flatten[Reap[For[i = 1, i < 4, i++, Sow[i*4, 5, 6]]]], 1]
However, I think this is overly complicated -- and number 12 is in there two times. Isn't there a much simpler way to do this? Multiply two lists?
list-manipulation
Union @@ Outer[Times, 1, 2, 3, 4, 5, 6]
– mathe
Aug 31 at 3:18
add a comment |Â
up vote
6
down vote
favorite
up vote
6
down vote
favorite
I have two lists, e.g.
a = 1,2,3
b = 4,5,6
I'd like to multiply each element from the first list with each element from the second, like this:
1*4, 2*4, 3*4, 1*5, 2*5, 3*5, 1*6, 2*6, 3*6
I only need the result:
4, 5, 6, 8, 10, 12, 15, 18
I have found a solution for this:
Drop[Flatten[Reap[For[i = 1, i < 4, i++, Sow[i*4, 5, 6]]]], 1]
However, I think this is overly complicated -- and number 12 is in there two times. Isn't there a much simpler way to do this? Multiply two lists?
list-manipulation
I have two lists, e.g.
a = 1,2,3
b = 4,5,6
I'd like to multiply each element from the first list with each element from the second, like this:
1*4, 2*4, 3*4, 1*5, 2*5, 3*5, 1*6, 2*6, 3*6
I only need the result:
4, 5, 6, 8, 10, 12, 15, 18
I have found a solution for this:
Drop[Flatten[Reap[For[i = 1, i < 4, i++, Sow[i*4, 5, 6]]]], 1]
However, I think this is overly complicated -- and number 12 is in there two times. Isn't there a much simpler way to do this? Multiply two lists?
list-manipulation
asked Aug 30 at 18:47
Marian Stiehler
585
585
Union @@ Outer[Times, 1, 2, 3, 4, 5, 6]
– mathe
Aug 31 at 3:18
add a comment |Â
Union @@ Outer[Times, 1, 2, 3, 4, 5, 6]
– mathe
Aug 31 at 3:18
Union @@ Outer[Times, 1, 2, 3, 4, 5, 6]
– mathe
Aug 31 at 3:18
Union @@ Outer[Times, 1, 2, 3, 4, 5, 6]
– mathe
Aug 31 at 3:18
add a comment |Â
3 Answers
3
active
oldest
votes
up vote
2
down vote
accepted
Another way to produce the result you seek is:
DeleteDuplicates[Flatten[Table[i*j, i, a, j, b]]]
4, 5, 6, 8, 10, 12, 15, 18
This will also work, and generalizes easily to more than two lists:
DeleteDuplicates[Flatten[TensorProduct[a, b]]]
TensorProduct
runs in time similar to KronekerProduct
and Outer
.
add a comment |Â
up vote
9
down vote
a = 1, 2, 3;
b = 4, 5, 6;
Flatten[KroneckerProduct[a, b]]
4, 5, 6, 8, 10, 12, 12, 15, 18
Use DeleteDuplicates
to, well, delete duplicates.
add a comment |Â
up vote
9
down vote
DeleteDuplicates @ Flatten @ Outer[Times, a, b]
4, 5, 6, 8, 10, 12, 15, 18
Also, shorter but much slower
DeleteDuplicates[Times @@@ Tuples @ a, b]
4, 5, 6, 8, 10, 12, 15, 18
Note: If speed is a concern, then Outer
, KroneckerProduct
and TensorProduct
are much faster than Table
and Tuples
methods:
SeedRandom[1]
a, b = RandomInteger[100, 2, 1500];
r1 = DeleteDuplicates @ Flatten @ Outer[Times, a, b]; // RepeatedTiming // First
0.0103
r2 = DeleteDuplicates[Times @@@ Tuples @ a, b]; // RepeatedTiming // First
0.92
r3 = DeleteDuplicates[Flatten[Table[i*j, i, a, j, b]]]; // RepeatedTiming // First
0.78
r4 = DeleteDuplicates @Flatten[KroneckerProduct[a, b]]; // RepeatedTiming // First ÂÂ
0.011
r5 = DeleteDuplicates[Flatten[TensorProduct[a, b]]]; // RepeatedTiming // First
0.011
r1 == r2 == r3 == r4 == r5
True
add a comment |Â
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
accepted
Another way to produce the result you seek is:
DeleteDuplicates[Flatten[Table[i*j, i, a, j, b]]]
4, 5, 6, 8, 10, 12, 15, 18
This will also work, and generalizes easily to more than two lists:
DeleteDuplicates[Flatten[TensorProduct[a, b]]]
TensorProduct
runs in time similar to KronekerProduct
and Outer
.
add a comment |Â
up vote
2
down vote
accepted
Another way to produce the result you seek is:
DeleteDuplicates[Flatten[Table[i*j, i, a, j, b]]]
4, 5, 6, 8, 10, 12, 15, 18
This will also work, and generalizes easily to more than two lists:
DeleteDuplicates[Flatten[TensorProduct[a, b]]]
TensorProduct
runs in time similar to KronekerProduct
and Outer
.
add a comment |Â
up vote
2
down vote
accepted
up vote
2
down vote
accepted
Another way to produce the result you seek is:
DeleteDuplicates[Flatten[Table[i*j, i, a, j, b]]]
4, 5, 6, 8, 10, 12, 15, 18
This will also work, and generalizes easily to more than two lists:
DeleteDuplicates[Flatten[TensorProduct[a, b]]]
TensorProduct
runs in time similar to KronekerProduct
and Outer
.
Another way to produce the result you seek is:
DeleteDuplicates[Flatten[Table[i*j, i, a, j, b]]]
4, 5, 6, 8, 10, 12, 15, 18
This will also work, and generalizes easily to more than two lists:
DeleteDuplicates[Flatten[TensorProduct[a, b]]]
TensorProduct
runs in time similar to KronekerProduct
and Outer
.
edited Aug 30 at 21:49
answered Aug 30 at 20:52


Lee
40817
40817
add a comment |Â
add a comment |Â
up vote
9
down vote
a = 1, 2, 3;
b = 4, 5, 6;
Flatten[KroneckerProduct[a, b]]
4, 5, 6, 8, 10, 12, 12, 15, 18
Use DeleteDuplicates
to, well, delete duplicates.
add a comment |Â
up vote
9
down vote
a = 1, 2, 3;
b = 4, 5, 6;
Flatten[KroneckerProduct[a, b]]
4, 5, 6, 8, 10, 12, 12, 15, 18
Use DeleteDuplicates
to, well, delete duplicates.
add a comment |Â
up vote
9
down vote
up vote
9
down vote
a = 1, 2, 3;
b = 4, 5, 6;
Flatten[KroneckerProduct[a, b]]
4, 5, 6, 8, 10, 12, 12, 15, 18
Use DeleteDuplicates
to, well, delete duplicates.
a = 1, 2, 3;
b = 4, 5, 6;
Flatten[KroneckerProduct[a, b]]
4, 5, 6, 8, 10, 12, 12, 15, 18
Use DeleteDuplicates
to, well, delete duplicates.
edited Aug 30 at 21:10
answered Aug 30 at 18:51


Henrik Schumacher
36.8k249103
36.8k249103
add a comment |Â
add a comment |Â
up vote
9
down vote
DeleteDuplicates @ Flatten @ Outer[Times, a, b]
4, 5, 6, 8, 10, 12, 15, 18
Also, shorter but much slower
DeleteDuplicates[Times @@@ Tuples @ a, b]
4, 5, 6, 8, 10, 12, 15, 18
Note: If speed is a concern, then Outer
, KroneckerProduct
and TensorProduct
are much faster than Table
and Tuples
methods:
SeedRandom[1]
a, b = RandomInteger[100, 2, 1500];
r1 = DeleteDuplicates @ Flatten @ Outer[Times, a, b]; // RepeatedTiming // First
0.0103
r2 = DeleteDuplicates[Times @@@ Tuples @ a, b]; // RepeatedTiming // First
0.92
r3 = DeleteDuplicates[Flatten[Table[i*j, i, a, j, b]]]; // RepeatedTiming // First
0.78
r4 = DeleteDuplicates @Flatten[KroneckerProduct[a, b]]; // RepeatedTiming // First ÂÂ
0.011
r5 = DeleteDuplicates[Flatten[TensorProduct[a, b]]]; // RepeatedTiming // First
0.011
r1 == r2 == r3 == r4 == r5
True
add a comment |Â
up vote
9
down vote
DeleteDuplicates @ Flatten @ Outer[Times, a, b]
4, 5, 6, 8, 10, 12, 15, 18
Also, shorter but much slower
DeleteDuplicates[Times @@@ Tuples @ a, b]
4, 5, 6, 8, 10, 12, 15, 18
Note: If speed is a concern, then Outer
, KroneckerProduct
and TensorProduct
are much faster than Table
and Tuples
methods:
SeedRandom[1]
a, b = RandomInteger[100, 2, 1500];
r1 = DeleteDuplicates @ Flatten @ Outer[Times, a, b]; // RepeatedTiming // First
0.0103
r2 = DeleteDuplicates[Times @@@ Tuples @ a, b]; // RepeatedTiming // First
0.92
r3 = DeleteDuplicates[Flatten[Table[i*j, i, a, j, b]]]; // RepeatedTiming // First
0.78
r4 = DeleteDuplicates @Flatten[KroneckerProduct[a, b]]; // RepeatedTiming // First ÂÂ
0.011
r5 = DeleteDuplicates[Flatten[TensorProduct[a, b]]]; // RepeatedTiming // First
0.011
r1 == r2 == r3 == r4 == r5
True
add a comment |Â
up vote
9
down vote
up vote
9
down vote
DeleteDuplicates @ Flatten @ Outer[Times, a, b]
4, 5, 6, 8, 10, 12, 15, 18
Also, shorter but much slower
DeleteDuplicates[Times @@@ Tuples @ a, b]
4, 5, 6, 8, 10, 12, 15, 18
Note: If speed is a concern, then Outer
, KroneckerProduct
and TensorProduct
are much faster than Table
and Tuples
methods:
SeedRandom[1]
a, b = RandomInteger[100, 2, 1500];
r1 = DeleteDuplicates @ Flatten @ Outer[Times, a, b]; // RepeatedTiming // First
0.0103
r2 = DeleteDuplicates[Times @@@ Tuples @ a, b]; // RepeatedTiming // First
0.92
r3 = DeleteDuplicates[Flatten[Table[i*j, i, a, j, b]]]; // RepeatedTiming // First
0.78
r4 = DeleteDuplicates @Flatten[KroneckerProduct[a, b]]; // RepeatedTiming // First ÂÂ
0.011
r5 = DeleteDuplicates[Flatten[TensorProduct[a, b]]]; // RepeatedTiming // First
0.011
r1 == r2 == r3 == r4 == r5
True
DeleteDuplicates @ Flatten @ Outer[Times, a, b]
4, 5, 6, 8, 10, 12, 15, 18
Also, shorter but much slower
DeleteDuplicates[Times @@@ Tuples @ a, b]
4, 5, 6, 8, 10, 12, 15, 18
Note: If speed is a concern, then Outer
, KroneckerProduct
and TensorProduct
are much faster than Table
and Tuples
methods:
SeedRandom[1]
a, b = RandomInteger[100, 2, 1500];
r1 = DeleteDuplicates @ Flatten @ Outer[Times, a, b]; // RepeatedTiming // First
0.0103
r2 = DeleteDuplicates[Times @@@ Tuples @ a, b]; // RepeatedTiming // First
0.92
r3 = DeleteDuplicates[Flatten[Table[i*j, i, a, j, b]]]; // RepeatedTiming // First
0.78
r4 = DeleteDuplicates @Flatten[KroneckerProduct[a, b]]; // RepeatedTiming // First ÂÂ
0.011
r5 = DeleteDuplicates[Flatten[TensorProduct[a, b]]]; // RepeatedTiming // First
0.011
r1 == r2 == r3 == r4 == r5
True
edited Aug 30 at 21:42
answered Aug 30 at 18:51
kglr
159k8183382
159k8183382
add a comment |Â
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmathematica.stackexchange.com%2fquestions%2f180916%2fmultiply-two-lists%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Union @@ Outer[Times, 1, 2, 3, 4, 5, 6]
– mathe
Aug 31 at 3:18