Help with sum specific elements of a matrix in mathematica

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
1
down vote

favorite












I am trying to sum over specific indices in a matrix.



For example, if I have the matrix



a = 1, 2, 3, 3, 4, 5, 6, 6, 7, 8, 9, 9, 1, 5, 9, 7


i need to sum the index 2, 1 and 4, 3 , i.e 4 + 9 = 13 automatically.



I try the code



Sum[a[[ii]][[jj]], ii, 2, 4, 2, jj, 1, 4 , 2]


and this is equals to 20. (WTF) (i think this shows the result of 4+6+1+9) Why?



In a general form I need to compute the sum of indices 2, 1, 4, 3, 6, 5, 8, 7, 10, 9, 12, 11, 14,13, 16,15 for a 16x16 square matrix.



Thanks for the help



Regards










share|improve this question















migrated from stats.stackexchange.com 49 mins ago


This question came from our site for people interested in statistics, machine learning, data analysis, data mining, and data visualization.


















    up vote
    1
    down vote

    favorite












    I am trying to sum over specific indices in a matrix.



    For example, if I have the matrix



    a = 1, 2, 3, 3, 4, 5, 6, 6, 7, 8, 9, 9, 1, 5, 9, 7


    i need to sum the index 2, 1 and 4, 3 , i.e 4 + 9 = 13 automatically.



    I try the code



    Sum[a[[ii]][[jj]], ii, 2, 4, 2, jj, 1, 4 , 2]


    and this is equals to 20. (WTF) (i think this shows the result of 4+6+1+9) Why?



    In a general form I need to compute the sum of indices 2, 1, 4, 3, 6, 5, 8, 7, 10, 9, 12, 11, 14,13, 16,15 for a 16x16 square matrix.



    Thanks for the help



    Regards










    share|improve this question















    migrated from stats.stackexchange.com 49 mins ago


    This question came from our site for people interested in statistics, machine learning, data analysis, data mining, and data visualization.
















      up vote
      1
      down vote

      favorite









      up vote
      1
      down vote

      favorite











      I am trying to sum over specific indices in a matrix.



      For example, if I have the matrix



      a = 1, 2, 3, 3, 4, 5, 6, 6, 7, 8, 9, 9, 1, 5, 9, 7


      i need to sum the index 2, 1 and 4, 3 , i.e 4 + 9 = 13 automatically.



      I try the code



      Sum[a[[ii]][[jj]], ii, 2, 4, 2, jj, 1, 4 , 2]


      and this is equals to 20. (WTF) (i think this shows the result of 4+6+1+9) Why?



      In a general form I need to compute the sum of indices 2, 1, 4, 3, 6, 5, 8, 7, 10, 9, 12, 11, 14,13, 16,15 for a 16x16 square matrix.



      Thanks for the help



      Regards










      share|improve this question















      I am trying to sum over specific indices in a matrix.



      For example, if I have the matrix



      a = 1, 2, 3, 3, 4, 5, 6, 6, 7, 8, 9, 9, 1, 5, 9, 7


      i need to sum the index 2, 1 and 4, 3 , i.e 4 + 9 = 13 automatically.



      I try the code



      Sum[a[[ii]][[jj]], ii, 2, 4, 2, jj, 1, 4 , 2]


      and this is equals to 20. (WTF) (i think this shows the result of 4+6+1+9) Why?



      In a general form I need to compute the sum of indices 2, 1, 4, 3, 6, 5, 8, 7, 10, 9, 12, 11, 14,13, 16,15 for a 16x16 square matrix.



      Thanks for the help



      Regards







      matrix






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited 35 mins ago









      kglr

      165k8188388




      165k8188388










      asked 1 hour ago







      Santiago Marín Agudelo











      migrated from stats.stackexchange.com 49 mins ago


      This question came from our site for people interested in statistics, machine learning, data analysis, data mining, and data visualization.






      migrated from stats.stackexchange.com 49 mins ago


      This question came from our site for people interested in statistics, machine learning, data analysis, data mining, and data visualization.






















          3 Answers
          3






          active

          oldest

          votes

















          up vote
          4
          down vote













          a = 1, 2, 3, 3, 4, 5, 6, 6, 7, 8, 9, 9, 1, 5, 9, 7;
          positions = 2, 1, 4, 3;
          Total@Extract[a, positions]





          share|improve this answer



























            up vote
            2
            down vote













            To see why it doesn't work as you expected, look at the indices that you generated.



            Table[ii, jj, ii, 2, 4, 2, jj, 1, 4, 2]

            (* 2, 1, 2, 3, 4, 1, 4, 3 *)


            You generated four indices so the sum was over four entries. Use



            a = 1, 2, 3, 3, 4, 5, 6, 6, 7, 8, 9, 9, 1, 5, 9, 7;

            Sum[a[[Sequence @@ i]], i, 2, 1, 4, 3]

            (* 13 *)





            share|improve this answer



























              up vote
              1
              down vote













              I would have used Extract if Alan had not posted it first.



              So, here is a way to use Sum:



              Sum[a[[## & @@ i]], i, 2, 1, 4, 3]



              13




              But that is also posted a minute earlier by Bob Hanlon.



              That leaves



              ☺ = ♯, ♯♯ [Function] +## & @@ (♯[[##]] & @@@ ♯♯);
              ☺[a, 2, 1, 4, 3]



              13




              and



              ☺☺ = +## & @@ (♯ [Function] #[[## & @@ ♯]]) /@ #2 &;
              ☺☺[a, 2, 1, 4, 3]



              13







              share|improve this answer






















              • "A monad is just a monoid in the category of endofunctors, what's the problem?"
                – kglr
                7 mins ago










              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: false,
              showLowRepImageUploadWarning: true,
              reputationToPostImages: null,
              bindNavPrevention: true,
              postfix: "",
              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%2f183720%2fhelp-with-sum-specific-elements-of-a-matrix-in-mathematica%23new-answer', 'question_page');

              );

              Post as a guest





























              3 Answers
              3






              active

              oldest

              votes








              3 Answers
              3






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes








              up vote
              4
              down vote













              a = 1, 2, 3, 3, 4, 5, 6, 6, 7, 8, 9, 9, 1, 5, 9, 7;
              positions = 2, 1, 4, 3;
              Total@Extract[a, positions]





              share|improve this answer
























                up vote
                4
                down vote













                a = 1, 2, 3, 3, 4, 5, 6, 6, 7, 8, 9, 9, 1, 5, 9, 7;
                positions = 2, 1, 4, 3;
                Total@Extract[a, positions]





                share|improve this answer






















                  up vote
                  4
                  down vote










                  up vote
                  4
                  down vote









                  a = 1, 2, 3, 3, 4, 5, 6, 6, 7, 8, 9, 9, 1, 5, 9, 7;
                  positions = 2, 1, 4, 3;
                  Total@Extract[a, positions]





                  share|improve this answer












                  a = 1, 2, 3, 3, 4, 5, 6, 6, 7, 8, 9, 9, 1, 5, 9, 7;
                  positions = 2, 1, 4, 3;
                  Total@Extract[a, positions]






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered 1 hour ago









                  Alan

                  5,8691023




                  5,8691023




















                      up vote
                      2
                      down vote













                      To see why it doesn't work as you expected, look at the indices that you generated.



                      Table[ii, jj, ii, 2, 4, 2, jj, 1, 4, 2]

                      (* 2, 1, 2, 3, 4, 1, 4, 3 *)


                      You generated four indices so the sum was over four entries. Use



                      a = 1, 2, 3, 3, 4, 5, 6, 6, 7, 8, 9, 9, 1, 5, 9, 7;

                      Sum[a[[Sequence @@ i]], i, 2, 1, 4, 3]

                      (* 13 *)





                      share|improve this answer
























                        up vote
                        2
                        down vote













                        To see why it doesn't work as you expected, look at the indices that you generated.



                        Table[ii, jj, ii, 2, 4, 2, jj, 1, 4, 2]

                        (* 2, 1, 2, 3, 4, 1, 4, 3 *)


                        You generated four indices so the sum was over four entries. Use



                        a = 1, 2, 3, 3, 4, 5, 6, 6, 7, 8, 9, 9, 1, 5, 9, 7;

                        Sum[a[[Sequence @@ i]], i, 2, 1, 4, 3]

                        (* 13 *)





                        share|improve this answer






















                          up vote
                          2
                          down vote










                          up vote
                          2
                          down vote









                          To see why it doesn't work as you expected, look at the indices that you generated.



                          Table[ii, jj, ii, 2, 4, 2, jj, 1, 4, 2]

                          (* 2, 1, 2, 3, 4, 1, 4, 3 *)


                          You generated four indices so the sum was over four entries. Use



                          a = 1, 2, 3, 3, 4, 5, 6, 6, 7, 8, 9, 9, 1, 5, 9, 7;

                          Sum[a[[Sequence @@ i]], i, 2, 1, 4, 3]

                          (* 13 *)





                          share|improve this answer












                          To see why it doesn't work as you expected, look at the indices that you generated.



                          Table[ii, jj, ii, 2, 4, 2, jj, 1, 4, 2]

                          (* 2, 1, 2, 3, 4, 1, 4, 3 *)


                          You generated four indices so the sum was over four entries. Use



                          a = 1, 2, 3, 3, 4, 5, 6, 6, 7, 8, 9, 9, 1, 5, 9, 7;

                          Sum[a[[Sequence @@ i]], i, 2, 1, 4, 3]

                          (* 13 *)






                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered 20 mins ago









                          Bob Hanlon

                          56.2k23590




                          56.2k23590




















                              up vote
                              1
                              down vote













                              I would have used Extract if Alan had not posted it first.



                              So, here is a way to use Sum:



                              Sum[a[[## & @@ i]], i, 2, 1, 4, 3]



                              13




                              But that is also posted a minute earlier by Bob Hanlon.



                              That leaves



                              ☺ = ♯, ♯♯ [Function] +## & @@ (♯[[##]] & @@@ ♯♯);
                              ☺[a, 2, 1, 4, 3]



                              13




                              and



                              ☺☺ = +## & @@ (♯ [Function] #[[## & @@ ♯]]) /@ #2 &;
                              ☺☺[a, 2, 1, 4, 3]



                              13







                              share|improve this answer






















                              • "A monad is just a monoid in the category of endofunctors, what's the problem?"
                                – kglr
                                7 mins ago














                              up vote
                              1
                              down vote













                              I would have used Extract if Alan had not posted it first.



                              So, here is a way to use Sum:



                              Sum[a[[## & @@ i]], i, 2, 1, 4, 3]



                              13




                              But that is also posted a minute earlier by Bob Hanlon.



                              That leaves



                              ☺ = ♯, ♯♯ [Function] +## & @@ (♯[[##]] & @@@ ♯♯);
                              ☺[a, 2, 1, 4, 3]



                              13




                              and



                              ☺☺ = +## & @@ (♯ [Function] #[[## & @@ ♯]]) /@ #2 &;
                              ☺☺[a, 2, 1, 4, 3]



                              13







                              share|improve this answer






















                              • "A monad is just a monoid in the category of endofunctors, what's the problem?"
                                – kglr
                                7 mins ago












                              up vote
                              1
                              down vote










                              up vote
                              1
                              down vote









                              I would have used Extract if Alan had not posted it first.



                              So, here is a way to use Sum:



                              Sum[a[[## & @@ i]], i, 2, 1, 4, 3]



                              13




                              But that is also posted a minute earlier by Bob Hanlon.



                              That leaves



                              ☺ = ♯, ♯♯ [Function] +## & @@ (♯[[##]] & @@@ ♯♯);
                              ☺[a, 2, 1, 4, 3]



                              13




                              and



                              ☺☺ = +## & @@ (♯ [Function] #[[## & @@ ♯]]) /@ #2 &;
                              ☺☺[a, 2, 1, 4, 3]



                              13







                              share|improve this answer














                              I would have used Extract if Alan had not posted it first.



                              So, here is a way to use Sum:



                              Sum[a[[## & @@ i]], i, 2, 1, 4, 3]



                              13




                              But that is also posted a minute earlier by Bob Hanlon.



                              That leaves



                              ☺ = ♯, ♯♯ [Function] +## & @@ (♯[[##]] & @@@ ♯♯);
                              ☺[a, 2, 1, 4, 3]



                              13




                              and



                              ☺☺ = +## & @@ (♯ [Function] #[[## & @@ ♯]]) /@ #2 &;
                              ☺☺[a, 2, 1, 4, 3]



                              13








                              share|improve this answer














                              share|improve this answer



                              share|improve this answer








                              edited 8 mins ago

























                              answered 19 mins ago









                              kglr

                              165k8188388




                              165k8188388











                              • "A monad is just a monoid in the category of endofunctors, what's the problem?"
                                – kglr
                                7 mins ago
















                              • "A monad is just a monoid in the category of endofunctors, what's the problem?"
                                – kglr
                                7 mins ago















                              "A monad is just a monoid in the category of endofunctors, what's the problem?"
                              – kglr
                              7 mins ago




                              "A monad is just a monoid in the category of endofunctors, what's the problem?"
                              – kglr
                              7 mins ago

















                               

                              draft saved


                              draft discarded















































                               


                              draft saved


                              draft discarded














                              StackExchange.ready(
                              function ()
                              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmathematica.stackexchange.com%2fquestions%2f183720%2fhelp-with-sum-specific-elements-of-a-matrix-in-mathematica%23new-answer', 'question_page');

                              );

                              Post as a guest













































































                              Popular posts from this blog

                              How to check contact read email or not when send email to Individual?

                              Displaying single band from multi-band raster using QGIS

                              How many registers does an x86_64 CPU actually have?