Custom NumPy Slice












1















I have a bunch of numpy arrays that can differ in shape:



[[1, 1, 1, 1, 1],
[1, 1, 1, 1, 1],
[1, 1, 1, 1, 1],
[1, 1, 1, 1, 1]]


I need to select and store the indices into a variable so that I can change the array into:



[[1, 1, 1, 1, 1],
[1, 1, 1, 1, 0],
[1, 1, 1, 1, 0],
[0, 0, 0, 0, 0]]


I can grab the vertical indices:



idx = np.s_[1:4, 3]


But I can't figure out how to add all of the indices from the last row and store them into idx



Update



I want the indices. There are times when I need to reference the values at those indices and there are times when I want to change the values at those indices. Having the indices will allow me the flexibility to do both.










share|improve this question




















  • 1





    I want the indices. There are times when I need to reference the values at those indices and there are times when I want to change the values at those indices. Having the indices will allow me the flexibility to do both.

    – slaw
    Jan 2 at 13:44
















1















I have a bunch of numpy arrays that can differ in shape:



[[1, 1, 1, 1, 1],
[1, 1, 1, 1, 1],
[1, 1, 1, 1, 1],
[1, 1, 1, 1, 1]]


I need to select and store the indices into a variable so that I can change the array into:



[[1, 1, 1, 1, 1],
[1, 1, 1, 1, 0],
[1, 1, 1, 1, 0],
[0, 0, 0, 0, 0]]


I can grab the vertical indices:



idx = np.s_[1:4, 3]


But I can't figure out how to add all of the indices from the last row and store them into idx



Update



I want the indices. There are times when I need to reference the values at those indices and there are times when I want to change the values at those indices. Having the indices will allow me the flexibility to do both.










share|improve this question




















  • 1





    I want the indices. There are times when I need to reference the values at those indices and there are times when I want to change the values at those indices. Having the indices will allow me the flexibility to do both.

    – slaw
    Jan 2 at 13:44














1












1








1


1






I have a bunch of numpy arrays that can differ in shape:



[[1, 1, 1, 1, 1],
[1, 1, 1, 1, 1],
[1, 1, 1, 1, 1],
[1, 1, 1, 1, 1]]


I need to select and store the indices into a variable so that I can change the array into:



[[1, 1, 1, 1, 1],
[1, 1, 1, 1, 0],
[1, 1, 1, 1, 0],
[0, 0, 0, 0, 0]]


I can grab the vertical indices:



idx = np.s_[1:4, 3]


But I can't figure out how to add all of the indices from the last row and store them into idx



Update



I want the indices. There are times when I need to reference the values at those indices and there are times when I want to change the values at those indices. Having the indices will allow me the flexibility to do both.










share|improve this question
















I have a bunch of numpy arrays that can differ in shape:



[[1, 1, 1, 1, 1],
[1, 1, 1, 1, 1],
[1, 1, 1, 1, 1],
[1, 1, 1, 1, 1]]


I need to select and store the indices into a variable so that I can change the array into:



[[1, 1, 1, 1, 1],
[1, 1, 1, 1, 0],
[1, 1, 1, 1, 0],
[0, 0, 0, 0, 0]]


I can grab the vertical indices:



idx = np.s_[1:4, 3]


But I can't figure out how to add all of the indices from the last row and store them into idx



Update



I want the indices. There are times when I need to reference the values at those indices and there are times when I want to change the values at those indices. Having the indices will allow me the flexibility to do both.







python numpy numpy-slicing






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 2 at 13:44







slaw

















asked Jan 2 at 13:31









slawslaw

1,54312454




1,54312454








  • 1





    I want the indices. There are times when I need to reference the values at those indices and there are times when I want to change the values at those indices. Having the indices will allow me the flexibility to do both.

    – slaw
    Jan 2 at 13:44














  • 1





    I want the indices. There are times when I need to reference the values at those indices and there are times when I want to change the values at those indices. Having the indices will allow me the flexibility to do both.

    – slaw
    Jan 2 at 13:44








1




1





I want the indices. There are times when I need to reference the values at those indices and there are times when I want to change the values at those indices. Having the indices will allow me the flexibility to do both.

– slaw
Jan 2 at 13:44





I want the indices. There are times when I need to reference the values at those indices and there are times when I want to change the values at those indices. Having the indices will allow me the flexibility to do both.

– slaw
Jan 2 at 13:44












2 Answers
2






active

oldest

votes


















0














I'm not aware of a built-in NumPy method, but perhaps this will do:



import numpy as np

a = np.random.rand(16).reshape((4, 4)) # Test matrix (4x4)
inds_a = np.arange(16).reshape((4, 4)) # Indices of a
idx = np.s_[0:3, 3] # Vertical indices
idy = np.s_[3, 0:3] # Horizontal indices

# Construct slice matrix
bools = np.zeros_like(a, dtype=bool)
bools[idx] = True
bools[idy] = True

print(a[bools]) # Select slice from matrix
print(inds_a[bools]) # Indices of sliced elements





share|improve this answer































    0














    This isn't quite using slices like you had, but numpy allows you to index with lists so you can store all the coordinates you want to change.



    A = np.ones((4,5))

    col = np.zeros(7,dtype='int')
    row = np.zeros(7,dtype='int')

    col[:5] = np.arange(5)
    col[5:] = 4

    row[:5] = 3
    row[5:] = np.arange(1,3)

    A[row,col] = 0


    You could also use two slices idx1 = np.s_[1:4,3] and idx2 = np.s_[3,0:5] and apply them both.






    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%2f54007291%2fcustom-numpy-slice%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









      0














      I'm not aware of a built-in NumPy method, but perhaps this will do:



      import numpy as np

      a = np.random.rand(16).reshape((4, 4)) # Test matrix (4x4)
      inds_a = np.arange(16).reshape((4, 4)) # Indices of a
      idx = np.s_[0:3, 3] # Vertical indices
      idy = np.s_[3, 0:3] # Horizontal indices

      # Construct slice matrix
      bools = np.zeros_like(a, dtype=bool)
      bools[idx] = True
      bools[idy] = True

      print(a[bools]) # Select slice from matrix
      print(inds_a[bools]) # Indices of sliced elements





      share|improve this answer




























        0














        I'm not aware of a built-in NumPy method, but perhaps this will do:



        import numpy as np

        a = np.random.rand(16).reshape((4, 4)) # Test matrix (4x4)
        inds_a = np.arange(16).reshape((4, 4)) # Indices of a
        idx = np.s_[0:3, 3] # Vertical indices
        idy = np.s_[3, 0:3] # Horizontal indices

        # Construct slice matrix
        bools = np.zeros_like(a, dtype=bool)
        bools[idx] = True
        bools[idy] = True

        print(a[bools]) # Select slice from matrix
        print(inds_a[bools]) # Indices of sliced elements





        share|improve this answer


























          0












          0








          0







          I'm not aware of a built-in NumPy method, but perhaps this will do:



          import numpy as np

          a = np.random.rand(16).reshape((4, 4)) # Test matrix (4x4)
          inds_a = np.arange(16).reshape((4, 4)) # Indices of a
          idx = np.s_[0:3, 3] # Vertical indices
          idy = np.s_[3, 0:3] # Horizontal indices

          # Construct slice matrix
          bools = np.zeros_like(a, dtype=bool)
          bools[idx] = True
          bools[idy] = True

          print(a[bools]) # Select slice from matrix
          print(inds_a[bools]) # Indices of sliced elements





          share|improve this answer













          I'm not aware of a built-in NumPy method, but perhaps this will do:



          import numpy as np

          a = np.random.rand(16).reshape((4, 4)) # Test matrix (4x4)
          inds_a = np.arange(16).reshape((4, 4)) # Indices of a
          idx = np.s_[0:3, 3] # Vertical indices
          idy = np.s_[3, 0:3] # Horizontal indices

          # Construct slice matrix
          bools = np.zeros_like(a, dtype=bool)
          bools[idx] = True
          bools[idy] = True

          print(a[bools]) # Select slice from matrix
          print(inds_a[bools]) # Indices of sliced elements






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Jan 2 at 14:01









          MPAMPA

          77011229




          77011229

























              0














              This isn't quite using slices like you had, but numpy allows you to index with lists so you can store all the coordinates you want to change.



              A = np.ones((4,5))

              col = np.zeros(7,dtype='int')
              row = np.zeros(7,dtype='int')

              col[:5] = np.arange(5)
              col[5:] = 4

              row[:5] = 3
              row[5:] = np.arange(1,3)

              A[row,col] = 0


              You could also use two slices idx1 = np.s_[1:4,3] and idx2 = np.s_[3,0:5] and apply them both.






              share|improve this answer






























                0














                This isn't quite using slices like you had, but numpy allows you to index with lists so you can store all the coordinates you want to change.



                A = np.ones((4,5))

                col = np.zeros(7,dtype='int')
                row = np.zeros(7,dtype='int')

                col[:5] = np.arange(5)
                col[5:] = 4

                row[:5] = 3
                row[5:] = np.arange(1,3)

                A[row,col] = 0


                You could also use two slices idx1 = np.s_[1:4,3] and idx2 = np.s_[3,0:5] and apply them both.






                share|improve this answer




























                  0












                  0








                  0







                  This isn't quite using slices like you had, but numpy allows you to index with lists so you can store all the coordinates you want to change.



                  A = np.ones((4,5))

                  col = np.zeros(7,dtype='int')
                  row = np.zeros(7,dtype='int')

                  col[:5] = np.arange(5)
                  col[5:] = 4

                  row[:5] = 3
                  row[5:] = np.arange(1,3)

                  A[row,col] = 0


                  You could also use two slices idx1 = np.s_[1:4,3] and idx2 = np.s_[3,0:5] and apply them both.






                  share|improve this answer















                  This isn't quite using slices like you had, but numpy allows you to index with lists so you can store all the coordinates you want to change.



                  A = np.ones((4,5))

                  col = np.zeros(7,dtype='int')
                  row = np.zeros(7,dtype='int')

                  col[:5] = np.arange(5)
                  col[5:] = 4

                  row[:5] = 3
                  row[5:] = np.arange(1,3)

                  A[row,col] = 0


                  You could also use two slices idx1 = np.s_[1:4,3] and idx2 = np.s_[3,0:5] and apply them both.







                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Jan 2 at 14:04

























                  answered Jan 2 at 13:54









                  tchtch

                  48525




                  48525






























                      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%2f54007291%2fcustom-numpy-slice%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

                      Monofisismo

                      Angular Downloading a file using contenturl with Basic Authentication

                      Olmecas