0

I have two arrays of (x,y) coordinates. After performing a mathematical operation on them, I ask the program which of the answers are less than 300.

I would also like it to tell me which two pairs of (x,y) coordinates (one from array a and one from array b) were used to compute this answer. How can I do this?

Nb: the arrays' contents are random and their lengths are chosen by the user (as you will see below).

Here is my code:

#Create arrays
xy_1 = [(random.randint(0,1000), random.randint(0,1000)) for h in range((int(raw_input(“Type your first array’s range: “)))]
a = array([xy_1])

xy_2 = #same as above
b = array([xy_2])

(If the user chooses for xy_1 to have 3 pairs and xy_2 to have 2 pairs, the output would look like this:

a[(1,5),(300,70),(10,435)]
b[(765,123),(456,20)]

with different numbers every time as it's a random number generator.)

#Perform mathematical operation
for element in a:
    for u in element:
        for element in b:
            for h in element:
                ans = (((u[0] - h[0])**2) + ((u[1] - h[1])**2))

                if ans <= 300:
#Problem here       print "a: " + str(np.nonzero(u)[0]) + "b: " + str(np.nonzero(h)[0]) + "is less than 300"
                else:
#and here           print "a: " + str(np.nonzero(u)[0]) + "b: " + str(np.nonzero(h)[0]) + "is more than 300"

Currently, the output looks like this:

a: [0 1] b: [0 1] is less than 300

or

a: [0 1] b: [0 1] is more than 300

but as I said above, I would like it to tell me the index (or whatever it's called for arrays) of each (x,y) pair in a and b, such that it looks like this:

a: 15 b: 3 is less than 300

(if it were the 15th pair of coordinates in a and the 3rd pair in b that created an outcome that was less than 300).

I have tried using zip with itertools.count, but it wound up repeating the iteration far too many times, so that's out.

  • possible duplicate of [Finding the index of an item given a list containing it in Python](http://stackoverflow.com/questions/176918/finding-the-index-of-an-item-given-a-list-containing-it-in-python) – sashkello Feb 07 '14 at 00:00
  • Thanks for the suggestion but I've tried index, enumerate and itertools, as the post's answers suggest, and none of them have worked so far. – YerABlizzardHarry Feb 07 '14 at 00:27
  • Then check out this: http://stackoverflow.com/questions/946860/using-pythons-list-index-method-on-a-list-of-tuples-or-objects – sashkello Feb 07 '14 at 00:31

1 Answers1

0
import numpy as np

h = int(raw_input("Type your first array's range: "))
xy_1 = np.random.randint(0, 1000, (h, 2))
a = xy_1

h = int(raw_input("Type your first array's range: "))
xy_2 = np.random.randint(0, 1000, (h, 2))
b = xy_2
ans = (a[:, 0] - b[:, 0, None]) ** 2 + (a[:, 1] - b[:, 1, None]) ** 2

a_indexes, b_indexes = np.nonzero(ans < 300)

Simply loop over the resulting indexes to print them. Although it seems like you're calculating distances here, and should add in a np.sqrt of the result before checking of they're bellow 300.

M4rtini
  • 13,186
  • 4
  • 35
  • 42