2

I've been oddly bashing my head against this problem for several hours, and would appreciate any help!

I would like to create a (for example) 100x100 array in which each index is a (x,y) coordinate. The overall goal here is the following:

I have x,y coordinates and would like to arrange them in a 2D space so that I can use the np.diagonal function to return the (x,y) coordinates along a line. I'll then use those (x,y) points to compare particular values.

The first step here is actually creating the array and I just can't seem to do it.

3
  • Could you provide some example of the code you are trying to run? i.e. arr[(x,y)], arr[x][y], arr[x,y], ... Commented Nov 3, 2014 at 18:32
  • 3
    You need to use a 3D array of shape (100, 100, 2), e.g. a = np.empty((100, 100, 2)). Calling np.diagonal(a) will return an array of shape (2, 100). Commented Nov 3, 2014 at 20:04
  • Hmmm, I think I can use Jamie's suggestion and follow it up by using zip(np.diagonal(a)[0], np.diagonal(a)[1]) to get a single list of (x,y) coordinates. All I need to do is to make sure the X and Y values are input correctly into the 3D array. What I don't understand is why I need 100 boxes of 100x2 in order to do this. Commented Nov 4, 2014 at 19:36

3 Answers 3

1

I'm not sure about the numpy part of your request, but you can create the array like so:

coords = [[(y,x) for x in range(100)] for y in range(100)]
>>> coords[50][2]
(50,2)
Sign up to request clarification or add additional context in comments.

Comments

1

If you just want the values along the diagonal, why dont you just create a 1D list?

import numpy as np
xs = np.linspace(1,10,100) # assuming x goes form 1 to 10
ys = np.linspace(2,3, 100) # assuming y goes from 2 to 3
xy = zip(xs, ys)

You no longer need the 2d array and then call the diagonal.

1 Comment

So the issue here is that calling the diagonal returns a single value, not every value on the diagonal which is what I need
1

Working on Jaime's suggestion:

>>> x, y = numpy.mgrid[0:100, 0:100]
>>> z = numpy.array([x, y]).transpose([1,2,0])
>>> z[50, 2]
array([50,  2])

EDIT: Given an array of points p, of the shape (2, P), this is how you would find out which of these points are underneath diagonal n:

>>> d = numpy.diagonal(z, n)
>>> cond0 = p[0, None] < d[0, :, None]
>>> cond1 = p[1, None] < d[1, :, None]
>>> good_indices_full = numpy.where(numpy.logical_and(cond0, cond1))
>>> good_indices = good_indices_full[1]

(I prefer to work with "good_indices", i.e. write stuff like p[:, good_indices], rather than the full tuple of arrays that numpy.where gives back).

2 Comments

So, this returns a single value ([50,2]) which is a problem since I will later be evaluating whether a particular point is above or below the appropriate point on the diagonal. To do this, I need every value that falls on that diagonal and not just the first value.
for me, numpy.diagonal(z) returns an array of shape (2, 100), that you should be able to use in your comparison.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.