How to multiply nested lists by a list with the same length?
up vote
5
down vote
favorite
Here's the expected output:
{{a, b}, {c, d}, {e, f}} * {3, 4} = {{3a, 4b}, {3c, 4d}, {3e, 4f}}
However, the code above will multiply each element of the encompassing list (3 elements) by {3, 4}
(2 elements) and cause an error.
I've tried defining a function and using Map
, but I'm sure there's a more elegant way of doing this speedily, without needing to first define a function.
list-manipulation
add a comment |
up vote
5
down vote
favorite
Here's the expected output:
{{a, b}, {c, d}, {e, f}} * {3, 4} = {{3a, 4b}, {3c, 4d}, {3e, 4f}}
However, the code above will multiply each element of the encompassing list (3 elements) by {3, 4}
(2 elements) and cause an error.
I've tried defining a function and using Map
, but I'm sure there's a more elegant way of doing this speedily, without needing to first define a function.
list-manipulation
add a comment |
up vote
5
down vote
favorite
up vote
5
down vote
favorite
Here's the expected output:
{{a, b}, {c, d}, {e, f}} * {3, 4} = {{3a, 4b}, {3c, 4d}, {3e, 4f}}
However, the code above will multiply each element of the encompassing list (3 elements) by {3, 4}
(2 elements) and cause an error.
I've tried defining a function and using Map
, but I'm sure there's a more elegant way of doing this speedily, without needing to first define a function.
list-manipulation
Here's the expected output:
{{a, b}, {c, d}, {e, f}} * {3, 4} = {{3a, 4b}, {3c, 4d}, {3e, 4f}}
However, the code above will multiply each element of the encompassing list (3 elements) by {3, 4}
(2 elements) and cause an error.
I've tried defining a function and using Map
, but I'm sure there's a more elegant way of doing this speedily, without needing to first define a function.
list-manipulation
list-manipulation
edited Nov 28 at 5:11
kglr
174k8196401
174k8196401
asked Nov 28 at 4:53
WeavingBird1917
1545
1545
add a comment |
add a comment |
6 Answers
6
active
oldest
votes
up vote
10
down vote
accepted
{3, 4} # & /@ {{a, b}, {c, d}, {e, f}}
{{3 a, 4 b}, {3 c, 4 d}, {3 e, 4 f}}
Alternative forms:
Map[{3, 4} # &][{{a, b}, {c, d}, {e, f}}]
{{3 a, 4 b}, {3 c, 4 d}, {3 e, 4 f}}
Map[{3, 4} # &, {{a, b}, {c, d}, {e, f}}]
{{3 a, 4 b}, {3 c, 4 d}, {3 e, 4 f}}
2
Works perfectly. If anyone else is wondering, the multiplication sign is not required here, but you can also modify this to perform other basic operations such as addition by placing the operator between the slot:{3, 4} + #
. Or to add subtract square root:{3, 4} - Sqrt[#]
.
– WeavingBird1917
Nov 28 at 5:14
add a comment |
up vote
8
down vote
{{a, b}, {c, d}, {e, f}}.DiagonalMatrix[{3, 4}]
{{3 a, 4 b}, {3 c, 4 d}, {3 e, 4 f}}
add a comment |
up vote
4
down vote
A fairly straightforward method is to use ScalingTransform
:
ScalingTransform[{3,4}] @ {{a,b},{c,d},{e,f}}
{{3 a, 4 b}, {3 c, 4 d}, {3 e, 4 f}}
It is not as fast as using a double Transpose
(@Thies) or Dot
(@Henrik) though:
list = RandomReal[1, {10^6, 3}];
scale = RandomReal[1, 3];
r1 = list . DiagonalMatrix[scale]; //RepeatedTiming
r2 = Transpose[scale Transpose[list]]; //RepeatedTiming
r3 = ScalingTransform[scale] @ list; //RepeatedTiming
r1 === r2 === r3
{0.0075, Null}
{0.018, Null}
{0.2, Null}
True
add a comment |
up vote
4
down vote
Inner[Times, {{a, b}, {c, d}, {e, f}}, {3, 4}, List]
add a comment |
up vote
2
down vote
Dear @WeavingBird1917 you can use Table for your purpose.
list={{a, b}, {c, d}, {e, f}};
result=Table[
{3*list[[i,1]],4*list[[i,2]]}
,{i,1,Length[list]}
]
If you want to change multiplication with summation, subtraction or division, you need only change the * sign with +, - or /.
add a comment |
up vote
2
down vote
You can also use Transpose
to bring the matrix into a shape where the multiplication by {3,4}
vectorises over the columns and then Transpose
again to bring it back into the original shape:
Transpose[{3, 4} Transpose[{{a, b}, {c, d}, {e, f}}]]
{{3 a, 4 b}, {3 c, 4 d}, {3 e, 4 f}}
or if we want to save a level of paranthesis, we can use the Esctr
Esc shortcut to get the postfix version of Transpose
:
Transpose[{3, 4} {{a, b}, {c, d}, {e, f}}
Esctr
Esc]
{{3 a, 4 b}, {3 c, 4 d}, {3 e, 4 f}}
If we find ourselves using the double transpose pattern a lot we could also make this into a neat operator:
Transposed[f_Function] := Transpose[f[Transpose[#]]] &
Transposed[{3, 4} # &][{{a, b}, {c, d}, {e, f}}]
{{3 a, 4 b}, {3 c, 4 d}, {3 e, 4 f}}
add a comment |
6 Answers
6
active
oldest
votes
6 Answers
6
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
10
down vote
accepted
{3, 4} # & /@ {{a, b}, {c, d}, {e, f}}
{{3 a, 4 b}, {3 c, 4 d}, {3 e, 4 f}}
Alternative forms:
Map[{3, 4} # &][{{a, b}, {c, d}, {e, f}}]
{{3 a, 4 b}, {3 c, 4 d}, {3 e, 4 f}}
Map[{3, 4} # &, {{a, b}, {c, d}, {e, f}}]
{{3 a, 4 b}, {3 c, 4 d}, {3 e, 4 f}}
2
Works perfectly. If anyone else is wondering, the multiplication sign is not required here, but you can also modify this to perform other basic operations such as addition by placing the operator between the slot:{3, 4} + #
. Or to add subtract square root:{3, 4} - Sqrt[#]
.
– WeavingBird1917
Nov 28 at 5:14
add a comment |
up vote
10
down vote
accepted
{3, 4} # & /@ {{a, b}, {c, d}, {e, f}}
{{3 a, 4 b}, {3 c, 4 d}, {3 e, 4 f}}
Alternative forms:
Map[{3, 4} # &][{{a, b}, {c, d}, {e, f}}]
{{3 a, 4 b}, {3 c, 4 d}, {3 e, 4 f}}
Map[{3, 4} # &, {{a, b}, {c, d}, {e, f}}]
{{3 a, 4 b}, {3 c, 4 d}, {3 e, 4 f}}
2
Works perfectly. If anyone else is wondering, the multiplication sign is not required here, but you can also modify this to perform other basic operations such as addition by placing the operator between the slot:{3, 4} + #
. Or to add subtract square root:{3, 4} - Sqrt[#]
.
– WeavingBird1917
Nov 28 at 5:14
add a comment |
up vote
10
down vote
accepted
up vote
10
down vote
accepted
{3, 4} # & /@ {{a, b}, {c, d}, {e, f}}
{{3 a, 4 b}, {3 c, 4 d}, {3 e, 4 f}}
Alternative forms:
Map[{3, 4} # &][{{a, b}, {c, d}, {e, f}}]
{{3 a, 4 b}, {3 c, 4 d}, {3 e, 4 f}}
Map[{3, 4} # &, {{a, b}, {c, d}, {e, f}}]
{{3 a, 4 b}, {3 c, 4 d}, {3 e, 4 f}}
{3, 4} # & /@ {{a, b}, {c, d}, {e, f}}
{{3 a, 4 b}, {3 c, 4 d}, {3 e, 4 f}}
Alternative forms:
Map[{3, 4} # &][{{a, b}, {c, d}, {e, f}}]
{{3 a, 4 b}, {3 c, 4 d}, {3 e, 4 f}}
Map[{3, 4} # &, {{a, b}, {c, d}, {e, f}}]
{{3 a, 4 b}, {3 c, 4 d}, {3 e, 4 f}}
edited Nov 28 at 5:12
answered Nov 28 at 4:55
kglr
174k8196401
174k8196401
2
Works perfectly. If anyone else is wondering, the multiplication sign is not required here, but you can also modify this to perform other basic operations such as addition by placing the operator between the slot:{3, 4} + #
. Or to add subtract square root:{3, 4} - Sqrt[#]
.
– WeavingBird1917
Nov 28 at 5:14
add a comment |
2
Works perfectly. If anyone else is wondering, the multiplication sign is not required here, but you can also modify this to perform other basic operations such as addition by placing the operator between the slot:{3, 4} + #
. Or to add subtract square root:{3, 4} - Sqrt[#]
.
– WeavingBird1917
Nov 28 at 5:14
2
2
Works perfectly. If anyone else is wondering, the multiplication sign is not required here, but you can also modify this to perform other basic operations such as addition by placing the operator between the slot:
{3, 4} + #
. Or to add subtract square root: {3, 4} - Sqrt[#]
.– WeavingBird1917
Nov 28 at 5:14
Works perfectly. If anyone else is wondering, the multiplication sign is not required here, but you can also modify this to perform other basic operations such as addition by placing the operator between the slot:
{3, 4} + #
. Or to add subtract square root: {3, 4} - Sqrt[#]
.– WeavingBird1917
Nov 28 at 5:14
add a comment |
up vote
8
down vote
{{a, b}, {c, d}, {e, f}}.DiagonalMatrix[{3, 4}]
{{3 a, 4 b}, {3 c, 4 d}, {3 e, 4 f}}
add a comment |
up vote
8
down vote
{{a, b}, {c, d}, {e, f}}.DiagonalMatrix[{3, 4}]
{{3 a, 4 b}, {3 c, 4 d}, {3 e, 4 f}}
add a comment |
up vote
8
down vote
up vote
8
down vote
{{a, b}, {c, d}, {e, f}}.DiagonalMatrix[{3, 4}]
{{3 a, 4 b}, {3 c, 4 d}, {3 e, 4 f}}
{{a, b}, {c, d}, {e, f}}.DiagonalMatrix[{3, 4}]
{{3 a, 4 b}, {3 c, 4 d}, {3 e, 4 f}}
answered Nov 28 at 6:11
Henrik Schumacher
46.1k466132
46.1k466132
add a comment |
add a comment |
up vote
4
down vote
A fairly straightforward method is to use ScalingTransform
:
ScalingTransform[{3,4}] @ {{a,b},{c,d},{e,f}}
{{3 a, 4 b}, {3 c, 4 d}, {3 e, 4 f}}
It is not as fast as using a double Transpose
(@Thies) or Dot
(@Henrik) though:
list = RandomReal[1, {10^6, 3}];
scale = RandomReal[1, 3];
r1 = list . DiagonalMatrix[scale]; //RepeatedTiming
r2 = Transpose[scale Transpose[list]]; //RepeatedTiming
r3 = ScalingTransform[scale] @ list; //RepeatedTiming
r1 === r2 === r3
{0.0075, Null}
{0.018, Null}
{0.2, Null}
True
add a comment |
up vote
4
down vote
A fairly straightforward method is to use ScalingTransform
:
ScalingTransform[{3,4}] @ {{a,b},{c,d},{e,f}}
{{3 a, 4 b}, {3 c, 4 d}, {3 e, 4 f}}
It is not as fast as using a double Transpose
(@Thies) or Dot
(@Henrik) though:
list = RandomReal[1, {10^6, 3}];
scale = RandomReal[1, 3];
r1 = list . DiagonalMatrix[scale]; //RepeatedTiming
r2 = Transpose[scale Transpose[list]]; //RepeatedTiming
r3 = ScalingTransform[scale] @ list; //RepeatedTiming
r1 === r2 === r3
{0.0075, Null}
{0.018, Null}
{0.2, Null}
True
add a comment |
up vote
4
down vote
up vote
4
down vote
A fairly straightforward method is to use ScalingTransform
:
ScalingTransform[{3,4}] @ {{a,b},{c,d},{e,f}}
{{3 a, 4 b}, {3 c, 4 d}, {3 e, 4 f}}
It is not as fast as using a double Transpose
(@Thies) or Dot
(@Henrik) though:
list = RandomReal[1, {10^6, 3}];
scale = RandomReal[1, 3];
r1 = list . DiagonalMatrix[scale]; //RepeatedTiming
r2 = Transpose[scale Transpose[list]]; //RepeatedTiming
r3 = ScalingTransform[scale] @ list; //RepeatedTiming
r1 === r2 === r3
{0.0075, Null}
{0.018, Null}
{0.2, Null}
True
A fairly straightforward method is to use ScalingTransform
:
ScalingTransform[{3,4}] @ {{a,b},{c,d},{e,f}}
{{3 a, 4 b}, {3 c, 4 d}, {3 e, 4 f}}
It is not as fast as using a double Transpose
(@Thies) or Dot
(@Henrik) though:
list = RandomReal[1, {10^6, 3}];
scale = RandomReal[1, 3];
r1 = list . DiagonalMatrix[scale]; //RepeatedTiming
r2 = Transpose[scale Transpose[list]]; //RepeatedTiming
r3 = ScalingTransform[scale] @ list; //RepeatedTiming
r1 === r2 === r3
{0.0075, Null}
{0.018, Null}
{0.2, Null}
True
answered Nov 28 at 17:24
Carl Woll
65.9k285171
65.9k285171
add a comment |
add a comment |
up vote
4
down vote
Inner[Times, {{a, b}, {c, d}, {e, f}}, {3, 4}, List]
add a comment |
up vote
4
down vote
Inner[Times, {{a, b}, {c, d}, {e, f}}, {3, 4}, List]
add a comment |
up vote
4
down vote
up vote
4
down vote
Inner[Times, {{a, b}, {c, d}, {e, f}}, {3, 4}, List]
Inner[Times, {{a, b}, {c, d}, {e, f}}, {3, 4}, List]
answered Nov 28 at 19:59
Chris
48115
48115
add a comment |
add a comment |
up vote
2
down vote
Dear @WeavingBird1917 you can use Table for your purpose.
list={{a, b}, {c, d}, {e, f}};
result=Table[
{3*list[[i,1]],4*list[[i,2]]}
,{i,1,Length[list]}
]
If you want to change multiplication with summation, subtraction or division, you need only change the * sign with +, - or /.
add a comment |
up vote
2
down vote
Dear @WeavingBird1917 you can use Table for your purpose.
list={{a, b}, {c, d}, {e, f}};
result=Table[
{3*list[[i,1]],4*list[[i,2]]}
,{i,1,Length[list]}
]
If you want to change multiplication with summation, subtraction or division, you need only change the * sign with +, - or /.
add a comment |
up vote
2
down vote
up vote
2
down vote
Dear @WeavingBird1917 you can use Table for your purpose.
list={{a, b}, {c, d}, {e, f}};
result=Table[
{3*list[[i,1]],4*list[[i,2]]}
,{i,1,Length[list]}
]
If you want to change multiplication with summation, subtraction or division, you need only change the * sign with +, - or /.
Dear @WeavingBird1917 you can use Table for your purpose.
list={{a, b}, {c, d}, {e, f}};
result=Table[
{3*list[[i,1]],4*list[[i,2]]}
,{i,1,Length[list]}
]
If you want to change multiplication with summation, subtraction or division, you need only change the * sign with +, - or /.
answered Nov 28 at 5:05
Hadi Sobhani
1947
1947
add a comment |
add a comment |
up vote
2
down vote
You can also use Transpose
to bring the matrix into a shape where the multiplication by {3,4}
vectorises over the columns and then Transpose
again to bring it back into the original shape:
Transpose[{3, 4} Transpose[{{a, b}, {c, d}, {e, f}}]]
{{3 a, 4 b}, {3 c, 4 d}, {3 e, 4 f}}
or if we want to save a level of paranthesis, we can use the Esctr
Esc shortcut to get the postfix version of Transpose
:
Transpose[{3, 4} {{a, b}, {c, d}, {e, f}}
Esctr
Esc]
{{3 a, 4 b}, {3 c, 4 d}, {3 e, 4 f}}
If we find ourselves using the double transpose pattern a lot we could also make this into a neat operator:
Transposed[f_Function] := Transpose[f[Transpose[#]]] &
Transposed[{3, 4} # &][{{a, b}, {c, d}, {e, f}}]
{{3 a, 4 b}, {3 c, 4 d}, {3 e, 4 f}}
add a comment |
up vote
2
down vote
You can also use Transpose
to bring the matrix into a shape where the multiplication by {3,4}
vectorises over the columns and then Transpose
again to bring it back into the original shape:
Transpose[{3, 4} Transpose[{{a, b}, {c, d}, {e, f}}]]
{{3 a, 4 b}, {3 c, 4 d}, {3 e, 4 f}}
or if we want to save a level of paranthesis, we can use the Esctr
Esc shortcut to get the postfix version of Transpose
:
Transpose[{3, 4} {{a, b}, {c, d}, {e, f}}
Esctr
Esc]
{{3 a, 4 b}, {3 c, 4 d}, {3 e, 4 f}}
If we find ourselves using the double transpose pattern a lot we could also make this into a neat operator:
Transposed[f_Function] := Transpose[f[Transpose[#]]] &
Transposed[{3, 4} # &][{{a, b}, {c, d}, {e, f}}]
{{3 a, 4 b}, {3 c, 4 d}, {3 e, 4 f}}
add a comment |
up vote
2
down vote
up vote
2
down vote
You can also use Transpose
to bring the matrix into a shape where the multiplication by {3,4}
vectorises over the columns and then Transpose
again to bring it back into the original shape:
Transpose[{3, 4} Transpose[{{a, b}, {c, d}, {e, f}}]]
{{3 a, 4 b}, {3 c, 4 d}, {3 e, 4 f}}
or if we want to save a level of paranthesis, we can use the Esctr
Esc shortcut to get the postfix version of Transpose
:
Transpose[{3, 4} {{a, b}, {c, d}, {e, f}}
Esctr
Esc]
{{3 a, 4 b}, {3 c, 4 d}, {3 e, 4 f}}
If we find ourselves using the double transpose pattern a lot we could also make this into a neat operator:
Transposed[f_Function] := Transpose[f[Transpose[#]]] &
Transposed[{3, 4} # &][{{a, b}, {c, d}, {e, f}}]
{{3 a, 4 b}, {3 c, 4 d}, {3 e, 4 f}}
You can also use Transpose
to bring the matrix into a shape where the multiplication by {3,4}
vectorises over the columns and then Transpose
again to bring it back into the original shape:
Transpose[{3, 4} Transpose[{{a, b}, {c, d}, {e, f}}]]
{{3 a, 4 b}, {3 c, 4 d}, {3 e, 4 f}}
or if we want to save a level of paranthesis, we can use the Esctr
Esc shortcut to get the postfix version of Transpose
:
Transpose[{3, 4} {{a, b}, {c, d}, {e, f}}
Esctr
Esc]
{{3 a, 4 b}, {3 c, 4 d}, {3 e, 4 f}}
If we find ourselves using the double transpose pattern a lot we could also make this into a neat operator:
Transposed[f_Function] := Transpose[f[Transpose[#]]] &
Transposed[{3, 4} # &][{{a, b}, {c, d}, {e, f}}]
{{3 a, 4 b}, {3 c, 4 d}, {3 e, 4 f}}
edited Nov 28 at 16:25
answered Nov 28 at 16:07
Thies Heidecke
6,7262438
6,7262438
add a comment |
add a comment |
Thanks for contributing an answer to Mathematica Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
Use MathJax to format equations. MathJax reference.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
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
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmathematica.stackexchange.com%2fquestions%2f186847%2fhow-to-multiply-nested-lists-by-a-list-with-the-same-length%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
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
Required, but never shown
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
Required, but never shown
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
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown