Update numpy at each index based on the previous index





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







0















Is there a vectorized way of updating each arbitrary index in a numpy array based on the indices before it? For example, in pseudo code, if I have the matrix



1 2 3
3 1 4
1 3 2


And for every index (i, j), i want to do:



m[i,j] += max(m[i, j-1], m[i-1, j])


Now I know I can do this iteratively, but I want to know if there's a vectorized way to do this, since it would be more efficient than taking it out of the numpy data space over and over again.



Also, I know this is a fence posting problem, since m[0, 0] doesn't have a previous element. This is easily fixed by prepending an extra row and column of 0's to the matrix.










share|improve this question























  • consider using numba

    – juanpa.arrivillaga
    Nov 23 '18 at 20:45


















0















Is there a vectorized way of updating each arbitrary index in a numpy array based on the indices before it? For example, in pseudo code, if I have the matrix



1 2 3
3 1 4
1 3 2


And for every index (i, j), i want to do:



m[i,j] += max(m[i, j-1], m[i-1, j])


Now I know I can do this iteratively, but I want to know if there's a vectorized way to do this, since it would be more efficient than taking it out of the numpy data space over and over again.



Also, I know this is a fence posting problem, since m[0, 0] doesn't have a previous element. This is easily fixed by prepending an extra row and column of 0's to the matrix.










share|improve this question























  • consider using numba

    – juanpa.arrivillaga
    Nov 23 '18 at 20:45














0












0








0


1






Is there a vectorized way of updating each arbitrary index in a numpy array based on the indices before it? For example, in pseudo code, if I have the matrix



1 2 3
3 1 4
1 3 2


And for every index (i, j), i want to do:



m[i,j] += max(m[i, j-1], m[i-1, j])


Now I know I can do this iteratively, but I want to know if there's a vectorized way to do this, since it would be more efficient than taking it out of the numpy data space over and over again.



Also, I know this is a fence posting problem, since m[0, 0] doesn't have a previous element. This is easily fixed by prepending an extra row and column of 0's to the matrix.










share|improve this question














Is there a vectorized way of updating each arbitrary index in a numpy array based on the indices before it? For example, in pseudo code, if I have the matrix



1 2 3
3 1 4
1 3 2


And for every index (i, j), i want to do:



m[i,j] += max(m[i, j-1], m[i-1, j])


Now I know I can do this iteratively, but I want to know if there's a vectorized way to do this, since it would be more efficient than taking it out of the numpy data space over and over again.



Also, I know this is a fence posting problem, since m[0, 0] doesn't have a previous element. This is easily fixed by prepending an extra row and column of 0's to the matrix.







python numpy






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 23 '18 at 20:35









Mauricio MartinezMauricio Martinez

206




206













  • consider using numba

    – juanpa.arrivillaga
    Nov 23 '18 at 20:45



















  • consider using numba

    – juanpa.arrivillaga
    Nov 23 '18 at 20:45

















consider using numba

– juanpa.arrivillaga
Nov 23 '18 at 20:45





consider using numba

– juanpa.arrivillaga
Nov 23 '18 at 20:45












2 Answers
2






active

oldest

votes


















3














Here's a way you could vectorize it:



arr = np.array([[1, 2, 3],[3, 1, 4],[1, 3, 2]])

arr_A = np.roll(arr, 1, axis=0)
arr_B = np.roll(arr, 1, axis=1)

max_val = np.maximum(arr_A, arr_B)

output = arr + max_val
>>> [[4 5 5]
[7 4 7]
[4 4 6]]


Note that this gives a different answer to your code above because the way you have it written means the values are updated after every loop. If you want that, then you are tied to using the for loops.



>>> [[ 4  6  9] # Output after updating the matrix in each loop.
[ 7 8 13]
[ 8 11 15]]


If you are looking for a similar kind of algorithm rather than trying to recover this exact output then np.roll() should work to speed things up.






share|improve this answer































    1














    You can use numpy.roll to created shifted versions of the matrix:



    m += np.maximum(np.roll(m, 1, axis=0), np.roll(m, 1, axis=1))


    This creates two new copies though. Zero padding is required because roll re-introduces elements that "rolled" beyond the boundary:



    p = np.pad(m, [(1, 1), (1, 1)], 'constant')
    m += np.maximum(np.roll(p, 1, axis=0), np.roll(p, 1, axis=1))[1:-1, 1:-1]





    share|improve this answer


























      Your Answer






      StackExchange.ifUsing("editor", function () {
      StackExchange.using("externalEditor", function () {
      StackExchange.using("snippets", function () {
      StackExchange.snippets.init();
      });
      });
      }, "code-snippets");

      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "1"
      };
      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',
      autoActivateHeartbeat: false,
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      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%2fstackoverflow.com%2fquestions%2f53452683%2fupdate-numpy-at-each-index-based-on-the-previous-index%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      3














      Here's a way you could vectorize it:



      arr = np.array([[1, 2, 3],[3, 1, 4],[1, 3, 2]])

      arr_A = np.roll(arr, 1, axis=0)
      arr_B = np.roll(arr, 1, axis=1)

      max_val = np.maximum(arr_A, arr_B)

      output = arr + max_val
      >>> [[4 5 5]
      [7 4 7]
      [4 4 6]]


      Note that this gives a different answer to your code above because the way you have it written means the values are updated after every loop. If you want that, then you are tied to using the for loops.



      >>> [[ 4  6  9] # Output after updating the matrix in each loop.
      [ 7 8 13]
      [ 8 11 15]]


      If you are looking for a similar kind of algorithm rather than trying to recover this exact output then np.roll() should work to speed things up.






      share|improve this answer




























        3














        Here's a way you could vectorize it:



        arr = np.array([[1, 2, 3],[3, 1, 4],[1, 3, 2]])

        arr_A = np.roll(arr, 1, axis=0)
        arr_B = np.roll(arr, 1, axis=1)

        max_val = np.maximum(arr_A, arr_B)

        output = arr + max_val
        >>> [[4 5 5]
        [7 4 7]
        [4 4 6]]


        Note that this gives a different answer to your code above because the way you have it written means the values are updated after every loop. If you want that, then you are tied to using the for loops.



        >>> [[ 4  6  9] # Output after updating the matrix in each loop.
        [ 7 8 13]
        [ 8 11 15]]


        If you are looking for a similar kind of algorithm rather than trying to recover this exact output then np.roll() should work to speed things up.






        share|improve this answer


























          3












          3








          3







          Here's a way you could vectorize it:



          arr = np.array([[1, 2, 3],[3, 1, 4],[1, 3, 2]])

          arr_A = np.roll(arr, 1, axis=0)
          arr_B = np.roll(arr, 1, axis=1)

          max_val = np.maximum(arr_A, arr_B)

          output = arr + max_val
          >>> [[4 5 5]
          [7 4 7]
          [4 4 6]]


          Note that this gives a different answer to your code above because the way you have it written means the values are updated after every loop. If you want that, then you are tied to using the for loops.



          >>> [[ 4  6  9] # Output after updating the matrix in each loop.
          [ 7 8 13]
          [ 8 11 15]]


          If you are looking for a similar kind of algorithm rather than trying to recover this exact output then np.roll() should work to speed things up.






          share|improve this answer













          Here's a way you could vectorize it:



          arr = np.array([[1, 2, 3],[3, 1, 4],[1, 3, 2]])

          arr_A = np.roll(arr, 1, axis=0)
          arr_B = np.roll(arr, 1, axis=1)

          max_val = np.maximum(arr_A, arr_B)

          output = arr + max_val
          >>> [[4 5 5]
          [7 4 7]
          [4 4 6]]


          Note that this gives a different answer to your code above because the way you have it written means the values are updated after every loop. If you want that, then you are tied to using the for loops.



          >>> [[ 4  6  9] # Output after updating the matrix in each loop.
          [ 7 8 13]
          [ 8 11 15]]


          If you are looking for a similar kind of algorithm rather than trying to recover this exact output then np.roll() should work to speed things up.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 23 '18 at 21:00









          berkelemberkelem

          9632721




          9632721

























              1














              You can use numpy.roll to created shifted versions of the matrix:



              m += np.maximum(np.roll(m, 1, axis=0), np.roll(m, 1, axis=1))


              This creates two new copies though. Zero padding is required because roll re-introduces elements that "rolled" beyond the boundary:



              p = np.pad(m, [(1, 1), (1, 1)], 'constant')
              m += np.maximum(np.roll(p, 1, axis=0), np.roll(p, 1, axis=1))[1:-1, 1:-1]





              share|improve this answer






























                1














                You can use numpy.roll to created shifted versions of the matrix:



                m += np.maximum(np.roll(m, 1, axis=0), np.roll(m, 1, axis=1))


                This creates two new copies though. Zero padding is required because roll re-introduces elements that "rolled" beyond the boundary:



                p = np.pad(m, [(1, 1), (1, 1)], 'constant')
                m += np.maximum(np.roll(p, 1, axis=0), np.roll(p, 1, axis=1))[1:-1, 1:-1]





                share|improve this answer




























                  1












                  1








                  1







                  You can use numpy.roll to created shifted versions of the matrix:



                  m += np.maximum(np.roll(m, 1, axis=0), np.roll(m, 1, axis=1))


                  This creates two new copies though. Zero padding is required because roll re-introduces elements that "rolled" beyond the boundary:



                  p = np.pad(m, [(1, 1), (1, 1)], 'constant')
                  m += np.maximum(np.roll(p, 1, axis=0), np.roll(p, 1, axis=1))[1:-1, 1:-1]





                  share|improve this answer















                  You can use numpy.roll to created shifted versions of the matrix:



                  m += np.maximum(np.roll(m, 1, axis=0), np.roll(m, 1, axis=1))


                  This creates two new copies though. Zero padding is required because roll re-introduces elements that "rolled" beyond the boundary:



                  p = np.pad(m, [(1, 1), (1, 1)], 'constant')
                  m += np.maximum(np.roll(p, 1, axis=0), np.roll(p, 1, axis=1))[1:-1, 1:-1]






                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Nov 23 '18 at 21:09

























                  answered Nov 23 '18 at 21:02









                  a_guesta_guest

                  7,56231345




                  7,56231345






























                      draft saved

                      draft discarded




















































                      Thanks for contributing an answer to Stack Overflow!


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




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53452683%2fupdate-numpy-at-each-index-based-on-the-previous-index%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

                      If I really need a card on my start hand, how many mulligans make sense? [duplicate]

                      Alcedinidae

                      Can an atomic nucleus contain both particles and antiparticles? [duplicate]