Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 564437
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T12:45:59+00:00 2026-05-13T12:45:59+00:00

I am looking for the optimal (fastest) way to find the exact overlap between

  • 0

I am looking for the optimal (fastest) way to find the exact overlap between two arrays in numpy. Given two arrays x and y

x = array([1,0,3,0,5,0,7,4],dtype=int)
y = array([1,4,0,0,5,0,6,4],dtype=int)

What I want to get is, an array of the same length that contains only the numbers from both vectors that are equal:

array([1,0,0,0,5,0,0,4])

First I tried

x&y
array([1,0,0,0,5,0,6,4])

Then I realised that this is always true for two numbers if they are > 0.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T12:45:59+00:00Added an answer on May 13, 2026 at 12:45 pm
    result = numpy.where(x == y, x, 0)
    

    Have a look at numpy.where documentation for explanation. Basically, numpy.where(a, b, c), for a condition a returns an array of shape a, and with values from b or c, depending upon whether the corresponding element of a is true or not. b or c can be scalars.

    By the way, x & y is not necessarily “always true” for two positive numbers. It does bitwise-and for elements in x and y:

    x = numpy.array([2**p for p in xrange(10)])
    # x is [  1   2   4   8  16  32  64 128 256 512]
    y = x - 1
    # y is [  0   1   3   7  15  31  63 127 255 511]
    x & y
    # result: [0 0 0 0 0 0 0 0 0 0]
    

    This is because the bitwise representation of each element in x is of the form 1 followed by n zeros, and the corresponding element in y is n 1s. In general, for two non-zero numbers a and b, a & b may equal zero, or non-zero but not necessarily equal to either a or b.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am looking to find a way to take two objects that have identical
I am looking for a optimal way to store info about which users already
I am looking for advice as to the most optimal way to store and
I am looking to see if there is a smart or optimal way to
This is probably something very simple but I'm looking for the optimal way of
I'm looking for the optimal way to create a function that can accept no
I was looking for any suggestion or a good approach to handle messages between
I am looking for the optimal strategy to use STL containers (like std::map and
I am looking for a way to identify (i.e. encode and decode) a set
Is there a way to determine the optimal cache size (L1 and L2 possibly)

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.