numpy logical.xor on a 2D array












1















I have a 2D numpy array with only 0 and 255 values (created from a black and white image) that i'd like to XOR with another similar 2D array.



The dtype of these arrays is uint8 and their shapes are identical.



The only information and examples i've been able to find, so far, deal with 1D arrays.



Do I need to 'flatten' these 2D arrays before XOR'ing? If so, how is that done?










share|improve this question



























    1















    I have a 2D numpy array with only 0 and 255 values (created from a black and white image) that i'd like to XOR with another similar 2D array.



    The dtype of these arrays is uint8 and their shapes are identical.



    The only information and examples i've been able to find, so far, deal with 1D arrays.



    Do I need to 'flatten' these 2D arrays before XOR'ing? If so, how is that done?










    share|improve this question

























      1












      1








      1








      I have a 2D numpy array with only 0 and 255 values (created from a black and white image) that i'd like to XOR with another similar 2D array.



      The dtype of these arrays is uint8 and their shapes are identical.



      The only information and examples i've been able to find, so far, deal with 1D arrays.



      Do I need to 'flatten' these 2D arrays before XOR'ing? If so, how is that done?










      share|improve this question














      I have a 2D numpy array with only 0 and 255 values (created from a black and white image) that i'd like to XOR with another similar 2D array.



      The dtype of these arrays is uint8 and their shapes are identical.



      The only information and examples i've been able to find, so far, deal with 1D arrays.



      Do I need to 'flatten' these 2D arrays before XOR'ing? If so, how is that done?







      arrays numpy python-2.x






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 28 '18 at 1:46









      yodishyodish

      329312




      329312
























          1 Answer
          1






          active

          oldest

          votes


















          1














          numpy.logical_xor and numpy.bitwise_xor will work for 2-D arrays, as will the operators != and ^ (essentially logical and bitwise XOR, respectively).



          edit: I just noticed in your title you are looking for logical XOR, but I will leave the bitwise info there for reference in case it's helpful.



          Setup:



          a = np.random.choice([0,255], (5,5))
          b = np.random.choice([0,255], (5,5))

          >>> a
          array([[255, 255, 0, 255, 255],
          [255, 255, 0, 255, 0],
          [255, 0, 0, 0, 0],
          [ 0, 255, 255, 255, 255],
          [ 0, 0, 255, 0, 0]])
          >>> b
          array([[ 0, 255, 255, 255, 255],
          [255, 0, 0, 255, 0],
          [255, 0, 255, 0, 255],
          [ 0, 0, 0, 0, 0],
          [255, 0, 0, 0, 255]])


          Logical XOR:



          >>> np.logical_xor(a,b)
          array([[ True, False, True, False, False],
          [False, True, False, False, False],
          [False, False, True, False, True],
          [False, True, True, True, True],
          [ True, False, True, False, True]])

          # equivalently:
          >>> a!=b
          array([[ True, False, True, False, False],
          [False, True, False, False, False],
          [False, False, True, False, True],
          [False, True, True, True, True],
          [ True, False, True, False, True]])


          Bitwise XOR:



          >>> np.bitwise_xor(a,b)
          array([[255, 0, 255, 0, 0],
          [ 0, 255, 0, 0, 0],
          [ 0, 0, 255, 0, 255],
          [ 0, 255, 255, 255, 255],
          [255, 0, 255, 0, 255]])

          # equivalently:
          >>> a^b
          array([[255, 0, 255, 0, 0],
          [ 0, 255, 0, 0, 0],
          [ 0, 0, 255, 0, 255],
          [ 0, 255, 255, 255, 255],
          [255, 0, 255, 0, 255]])





          share|improve this answer





















          • 1





            It turns out, Bitwise XOR was what I was looking for ! :) I appreciate it!

            – yodish
            Nov 28 '18 at 2:03











          • is there a numpy function that returns the intersection of two 2D arrays?

            – yodish
            Nov 28 '18 at 2:07











          • What do you mean by the intersection in this case?

            – sacuL
            Nov 28 '18 at 2:08











          • Returning an array that contains 255 values only where they exist (in the same location) in both array1 and array2.

            – yodish
            Nov 28 '18 at 2:12








          • 1





            in your case you could do something like ((a == 255) & (b == 255)) * 255

            – sacuL
            Nov 28 '18 at 2:42











          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%2f53510860%2fnumpy-logical-xor-on-a-2d-array%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          1














          numpy.logical_xor and numpy.bitwise_xor will work for 2-D arrays, as will the operators != and ^ (essentially logical and bitwise XOR, respectively).



          edit: I just noticed in your title you are looking for logical XOR, but I will leave the bitwise info there for reference in case it's helpful.



          Setup:



          a = np.random.choice([0,255], (5,5))
          b = np.random.choice([0,255], (5,5))

          >>> a
          array([[255, 255, 0, 255, 255],
          [255, 255, 0, 255, 0],
          [255, 0, 0, 0, 0],
          [ 0, 255, 255, 255, 255],
          [ 0, 0, 255, 0, 0]])
          >>> b
          array([[ 0, 255, 255, 255, 255],
          [255, 0, 0, 255, 0],
          [255, 0, 255, 0, 255],
          [ 0, 0, 0, 0, 0],
          [255, 0, 0, 0, 255]])


          Logical XOR:



          >>> np.logical_xor(a,b)
          array([[ True, False, True, False, False],
          [False, True, False, False, False],
          [False, False, True, False, True],
          [False, True, True, True, True],
          [ True, False, True, False, True]])

          # equivalently:
          >>> a!=b
          array([[ True, False, True, False, False],
          [False, True, False, False, False],
          [False, False, True, False, True],
          [False, True, True, True, True],
          [ True, False, True, False, True]])


          Bitwise XOR:



          >>> np.bitwise_xor(a,b)
          array([[255, 0, 255, 0, 0],
          [ 0, 255, 0, 0, 0],
          [ 0, 0, 255, 0, 255],
          [ 0, 255, 255, 255, 255],
          [255, 0, 255, 0, 255]])

          # equivalently:
          >>> a^b
          array([[255, 0, 255, 0, 0],
          [ 0, 255, 0, 0, 0],
          [ 0, 0, 255, 0, 255],
          [ 0, 255, 255, 255, 255],
          [255, 0, 255, 0, 255]])





          share|improve this answer





















          • 1





            It turns out, Bitwise XOR was what I was looking for ! :) I appreciate it!

            – yodish
            Nov 28 '18 at 2:03











          • is there a numpy function that returns the intersection of two 2D arrays?

            – yodish
            Nov 28 '18 at 2:07











          • What do you mean by the intersection in this case?

            – sacuL
            Nov 28 '18 at 2:08











          • Returning an array that contains 255 values only where they exist (in the same location) in both array1 and array2.

            – yodish
            Nov 28 '18 at 2:12








          • 1





            in your case you could do something like ((a == 255) & (b == 255)) * 255

            – sacuL
            Nov 28 '18 at 2:42
















          1














          numpy.logical_xor and numpy.bitwise_xor will work for 2-D arrays, as will the operators != and ^ (essentially logical and bitwise XOR, respectively).



          edit: I just noticed in your title you are looking for logical XOR, but I will leave the bitwise info there for reference in case it's helpful.



          Setup:



          a = np.random.choice([0,255], (5,5))
          b = np.random.choice([0,255], (5,5))

          >>> a
          array([[255, 255, 0, 255, 255],
          [255, 255, 0, 255, 0],
          [255, 0, 0, 0, 0],
          [ 0, 255, 255, 255, 255],
          [ 0, 0, 255, 0, 0]])
          >>> b
          array([[ 0, 255, 255, 255, 255],
          [255, 0, 0, 255, 0],
          [255, 0, 255, 0, 255],
          [ 0, 0, 0, 0, 0],
          [255, 0, 0, 0, 255]])


          Logical XOR:



          >>> np.logical_xor(a,b)
          array([[ True, False, True, False, False],
          [False, True, False, False, False],
          [False, False, True, False, True],
          [False, True, True, True, True],
          [ True, False, True, False, True]])

          # equivalently:
          >>> a!=b
          array([[ True, False, True, False, False],
          [False, True, False, False, False],
          [False, False, True, False, True],
          [False, True, True, True, True],
          [ True, False, True, False, True]])


          Bitwise XOR:



          >>> np.bitwise_xor(a,b)
          array([[255, 0, 255, 0, 0],
          [ 0, 255, 0, 0, 0],
          [ 0, 0, 255, 0, 255],
          [ 0, 255, 255, 255, 255],
          [255, 0, 255, 0, 255]])

          # equivalently:
          >>> a^b
          array([[255, 0, 255, 0, 0],
          [ 0, 255, 0, 0, 0],
          [ 0, 0, 255, 0, 255],
          [ 0, 255, 255, 255, 255],
          [255, 0, 255, 0, 255]])





          share|improve this answer





















          • 1





            It turns out, Bitwise XOR was what I was looking for ! :) I appreciate it!

            – yodish
            Nov 28 '18 at 2:03











          • is there a numpy function that returns the intersection of two 2D arrays?

            – yodish
            Nov 28 '18 at 2:07











          • What do you mean by the intersection in this case?

            – sacuL
            Nov 28 '18 at 2:08











          • Returning an array that contains 255 values only where they exist (in the same location) in both array1 and array2.

            – yodish
            Nov 28 '18 at 2:12








          • 1





            in your case you could do something like ((a == 255) & (b == 255)) * 255

            – sacuL
            Nov 28 '18 at 2:42














          1












          1








          1







          numpy.logical_xor and numpy.bitwise_xor will work for 2-D arrays, as will the operators != and ^ (essentially logical and bitwise XOR, respectively).



          edit: I just noticed in your title you are looking for logical XOR, but I will leave the bitwise info there for reference in case it's helpful.



          Setup:



          a = np.random.choice([0,255], (5,5))
          b = np.random.choice([0,255], (5,5))

          >>> a
          array([[255, 255, 0, 255, 255],
          [255, 255, 0, 255, 0],
          [255, 0, 0, 0, 0],
          [ 0, 255, 255, 255, 255],
          [ 0, 0, 255, 0, 0]])
          >>> b
          array([[ 0, 255, 255, 255, 255],
          [255, 0, 0, 255, 0],
          [255, 0, 255, 0, 255],
          [ 0, 0, 0, 0, 0],
          [255, 0, 0, 0, 255]])


          Logical XOR:



          >>> np.logical_xor(a,b)
          array([[ True, False, True, False, False],
          [False, True, False, False, False],
          [False, False, True, False, True],
          [False, True, True, True, True],
          [ True, False, True, False, True]])

          # equivalently:
          >>> a!=b
          array([[ True, False, True, False, False],
          [False, True, False, False, False],
          [False, False, True, False, True],
          [False, True, True, True, True],
          [ True, False, True, False, True]])


          Bitwise XOR:



          >>> np.bitwise_xor(a,b)
          array([[255, 0, 255, 0, 0],
          [ 0, 255, 0, 0, 0],
          [ 0, 0, 255, 0, 255],
          [ 0, 255, 255, 255, 255],
          [255, 0, 255, 0, 255]])

          # equivalently:
          >>> a^b
          array([[255, 0, 255, 0, 0],
          [ 0, 255, 0, 0, 0],
          [ 0, 0, 255, 0, 255],
          [ 0, 255, 255, 255, 255],
          [255, 0, 255, 0, 255]])





          share|improve this answer















          numpy.logical_xor and numpy.bitwise_xor will work for 2-D arrays, as will the operators != and ^ (essentially logical and bitwise XOR, respectively).



          edit: I just noticed in your title you are looking for logical XOR, but I will leave the bitwise info there for reference in case it's helpful.



          Setup:



          a = np.random.choice([0,255], (5,5))
          b = np.random.choice([0,255], (5,5))

          >>> a
          array([[255, 255, 0, 255, 255],
          [255, 255, 0, 255, 0],
          [255, 0, 0, 0, 0],
          [ 0, 255, 255, 255, 255],
          [ 0, 0, 255, 0, 0]])
          >>> b
          array([[ 0, 255, 255, 255, 255],
          [255, 0, 0, 255, 0],
          [255, 0, 255, 0, 255],
          [ 0, 0, 0, 0, 0],
          [255, 0, 0, 0, 255]])


          Logical XOR:



          >>> np.logical_xor(a,b)
          array([[ True, False, True, False, False],
          [False, True, False, False, False],
          [False, False, True, False, True],
          [False, True, True, True, True],
          [ True, False, True, False, True]])

          # equivalently:
          >>> a!=b
          array([[ True, False, True, False, False],
          [False, True, False, False, False],
          [False, False, True, False, True],
          [False, True, True, True, True],
          [ True, False, True, False, True]])


          Bitwise XOR:



          >>> np.bitwise_xor(a,b)
          array([[255, 0, 255, 0, 0],
          [ 0, 255, 0, 0, 0],
          [ 0, 0, 255, 0, 255],
          [ 0, 255, 255, 255, 255],
          [255, 0, 255, 0, 255]])

          # equivalently:
          >>> a^b
          array([[255, 0, 255, 0, 0],
          [ 0, 255, 0, 0, 0],
          [ 0, 0, 255, 0, 255],
          [ 0, 255, 255, 255, 255],
          [255, 0, 255, 0, 255]])






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 28 '18 at 1:54

























          answered Nov 28 '18 at 1:49









          sacuLsacuL

          30.6k41943




          30.6k41943








          • 1





            It turns out, Bitwise XOR was what I was looking for ! :) I appreciate it!

            – yodish
            Nov 28 '18 at 2:03











          • is there a numpy function that returns the intersection of two 2D arrays?

            – yodish
            Nov 28 '18 at 2:07











          • What do you mean by the intersection in this case?

            – sacuL
            Nov 28 '18 at 2:08











          • Returning an array that contains 255 values only where they exist (in the same location) in both array1 and array2.

            – yodish
            Nov 28 '18 at 2:12








          • 1





            in your case you could do something like ((a == 255) & (b == 255)) * 255

            – sacuL
            Nov 28 '18 at 2:42














          • 1





            It turns out, Bitwise XOR was what I was looking for ! :) I appreciate it!

            – yodish
            Nov 28 '18 at 2:03











          • is there a numpy function that returns the intersection of two 2D arrays?

            – yodish
            Nov 28 '18 at 2:07











          • What do you mean by the intersection in this case?

            – sacuL
            Nov 28 '18 at 2:08











          • Returning an array that contains 255 values only where they exist (in the same location) in both array1 and array2.

            – yodish
            Nov 28 '18 at 2:12








          • 1





            in your case you could do something like ((a == 255) & (b == 255)) * 255

            – sacuL
            Nov 28 '18 at 2:42








          1




          1





          It turns out, Bitwise XOR was what I was looking for ! :) I appreciate it!

          – yodish
          Nov 28 '18 at 2:03





          It turns out, Bitwise XOR was what I was looking for ! :) I appreciate it!

          – yodish
          Nov 28 '18 at 2:03













          is there a numpy function that returns the intersection of two 2D arrays?

          – yodish
          Nov 28 '18 at 2:07





          is there a numpy function that returns the intersection of two 2D arrays?

          – yodish
          Nov 28 '18 at 2:07













          What do you mean by the intersection in this case?

          – sacuL
          Nov 28 '18 at 2:08





          What do you mean by the intersection in this case?

          – sacuL
          Nov 28 '18 at 2:08













          Returning an array that contains 255 values only where they exist (in the same location) in both array1 and array2.

          – yodish
          Nov 28 '18 at 2:12







          Returning an array that contains 255 values only where they exist (in the same location) in both array1 and array2.

          – yodish
          Nov 28 '18 at 2:12






          1




          1





          in your case you could do something like ((a == 255) & (b == 255)) * 255

          – sacuL
          Nov 28 '18 at 2:42





          in your case you could do something like ((a == 255) & (b == 255)) * 255

          – sacuL
          Nov 28 '18 at 2:42




















          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%2f53510860%2fnumpy-logical-xor-on-a-2d-array%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

          Lallio

          Unable to find Lightning Node

          Futebolista