How to multiply nested lists by a list with the same length?











up vote
2
down vote

favorite
1












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.










share|improve this question




























    up vote
    2
    down vote

    favorite
    1












    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.










    share|improve this question


























      up vote
      2
      down vote

      favorite
      1









      up vote
      2
      down vote

      favorite
      1






      1





      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.










      share|improve this question















      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






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited 1 hour ago









      kglr

      173k8195400




      173k8195400










      asked 1 hour ago









      WeavingBird1917

      1395




      1395






















          3 Answers
          3






          active

          oldest

          votes

















          up vote
          4
          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}}







          share|improve this answer



















          • 1




            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
            1 hour ago




















          up vote
          1
          down vote













          {{a, b}, {c, d}, {e, f}}.DiagonalMatrix[{3, 4}]



          {{3 a, 4 b}, {3 c, 4 d}, {3 e, 4 f}}







          share|improve this answer




























            up vote
            0
            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 /.






            share|improve this answer





















              Your Answer





              StackExchange.ifUsing("editor", function () {
              return StackExchange.using("mathjaxEditing", function () {
              StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
              StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["$", "$"], ["\\(","\\)"]]);
              });
              });
              }, "mathjax-editing");

              StackExchange.ready(function() {
              var channelOptions = {
              tags: "".split(" "),
              id: "387"
              };
              initTagRenderer("".split(" "), "".split(" "), channelOptions);

              StackExchange.using("externalEditor", function() {
              // Have to fire editor after snippets, if snippets enabled
              if (StackExchange.settings.snippets.snippetsEnabled) {
              StackExchange.using("snippets", function() {
              createEditor();
              });
              }
              else {
              createEditor();
              }
              });

              function createEditor() {
              StackExchange.prepareEditor({
              heartbeatType: 'answer',
              convertImagesToLinks: false,
              noModals: true,
              showLowRepImageUploadWarning: true,
              reputationToPostImages: null,
              bindNavPrevention: true,
              postfix: "",
              imageUploader: {
              brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
              contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
              allowUrls: true
              },
              onDemand: true,
              discardSelector: ".discard-answer"
              ,immediatelyShowMarkdownHelp:true
              });


              }
              });














               

              draft saved


              draft discarded


















              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

























              3 Answers
              3






              active

              oldest

              votes








              3 Answers
              3






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes








              up vote
              4
              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}}







              share|improve this answer



















              • 1




                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
                1 hour ago

















              up vote
              4
              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}}







              share|improve this answer



















              • 1




                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
                1 hour ago















              up vote
              4
              down vote



              accepted







              up vote
              4
              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}}







              share|improve this answer














              {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}}








              share|improve this answer














              share|improve this answer



              share|improve this answer








              edited 1 hour ago

























              answered 1 hour ago









              kglr

              173k8195400




              173k8195400








              • 1




                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
                1 hour ago
















              • 1




                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
                1 hour ago










              1




              1




              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
              1 hour ago






              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
              1 hour ago












              up vote
              1
              down vote













              {{a, b}, {c, d}, {e, f}}.DiagonalMatrix[{3, 4}]



              {{3 a, 4 b}, {3 c, 4 d}, {3 e, 4 f}}







              share|improve this answer

























                up vote
                1
                down vote













                {{a, b}, {c, d}, {e, f}}.DiagonalMatrix[{3, 4}]



                {{3 a, 4 b}, {3 c, 4 d}, {3 e, 4 f}}







                share|improve this answer























                  up vote
                  1
                  down vote










                  up vote
                  1
                  down vote









                  {{a, b}, {c, d}, {e, f}}.DiagonalMatrix[{3, 4}]



                  {{3 a, 4 b}, {3 c, 4 d}, {3 e, 4 f}}







                  share|improve this answer












                  {{a, b}, {c, d}, {e, f}}.DiagonalMatrix[{3, 4}]



                  {{3 a, 4 b}, {3 c, 4 d}, {3 e, 4 f}}








                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered 30 mins ago









                  Henrik Schumacher

                  45.7k466132




                  45.7k466132






















                      up vote
                      0
                      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 /.






                      share|improve this answer

























                        up vote
                        0
                        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 /.






                        share|improve this answer























                          up vote
                          0
                          down vote










                          up vote
                          0
                          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 /.






                          share|improve this answer












                          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 /.







                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered 1 hour ago









                          Hadi Sobhani

                          1746




                          1746






























                               

                              draft saved


                              draft discarded



















































                               


                              draft saved


                              draft discarded














                              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





















































                              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







                              Popular posts from this blog

                              A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks

                              Calculate evaluation metrics using cross_val_predict sklearn

                              Insert data from modal to MySQL (multiple modal on website)