2

Consider a 100X100 array.

i) Generate an array of several thousand random locations within such an array, e.g. (3,75) and (56, 34).

ii) Calculate how often one of your random locations falls within 15 pixels of any of the (straight) edges.

I am trying to do the above question in order to help me to learn the programming language Python, i am new to programming.

Here is what i have got so far:

from __future__ import division
from pylab import *
import math as m
from numpy import *
from random import randrange

N = 3000
coords_array = array([randrange(100) for _ in range(2 * N)]).reshape(N, 2)

This creates the array of N random locations, and no i am trying to create a loop that will append a 1 to an empty list if x>85 or y>85 or x<15 or y<15, then append a zero to the same empty list if x or y is anything else. Then i would find the sum of the list, which would be my count of how many of the random location fall within the edges.

This is the kind of thing i am trying to do:

coordinate=coords_array[x,y]
b=[]
def location(x,y):
    if x>85 or y>85:
        b.appnend(1)
    if x<15 or y<15:
        b.append(1)
    else:
        b.append(0)


print b
print x

But i am having trouble assigning the array as x and y variables. I want to be able assign each row of the set of random coordinates as an x,y pair so that i can use it in my loop.

But i do not know how to do it!

Please can someone show me how to do it?

Thank you

3
  • 1
    Just a quick hint. You can generate the random locations like this: coords_array = randint(100, size = (3000, 2)). randint comes from numpy.random Commented Apr 10, 2013 at 12:07
  • so, you want the list to grow by two elements if it contains a coordinate greater than 85, but by one element otherwise? Is that intended? Commented Apr 10, 2013 at 12:13
  • This is one of three almost identical questions. In particular, what can you learn from this answer here? Commented Apr 10, 2013 at 12:43

2 Answers 2

1

Ok, the answer to this:

But i am having trouble assigning the array as x and y variables. I want to be able assign each row of the set of random coordinates as an x,y pair so that i can use it in my loop

Would be this:

for pair in coords_array:
    # Do something with the pair

NumPy arrays behave as regular Python sequences by letting for to iterate over their main axis, meaning pair will contain an array of (in your case) two elements: x and y. You can also do this:

for x, y in coords_array:
    # Do something with the pair

NB: I think you wanted to write the function like this:

def location(x,y):
    if x>85 or y>85:
        b.append(1)
    elif x<15 or y<15:
        b.append(1)
    else:
        b.append(0)

or

def location(x,y):
    if x>85 or y>85 or x<15 or y<15:
        b.append(1)
    else:
        b.append(0)

or even

def location(x,y):
    if not (15 <= x <= 85) or not (15 <= y <= 85):
        b.append(1)
    else:
        b.append(0)

Otherwise, as @TokenMacGuy points out, you'd be inserting two values in certain cases.

NB: from your question I understand you want to write this code specifically to learn Python, but you could do this in a much more straightforward (and efficient) way by just using NumPy functionality

Sign up to request clarification or add additional context in comments.

Comments

1

You can let numpy do the looping for you:

n = 3000
coords = np.random.randint(100, size=(n, 2))
x, y = coords.T
is_close_to_edge = (x < 15) | (x >= 85) | (y < 15) | (y >= 85)
count_close_to_edge = np.sum(is_close_to_edge)

Note that the first index of a 100 element array is 0 and the last 99, hence items within 15 positions of the edges are 0...14 and 85...99, hence the >= in the comparison. In the code above, is_close_to_edge is your list, with boolean values.

Comments

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.