Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I am having a small issue understanding indexing in Numpy arrays. I think a simplified example is best to get an idea of what I am trying to do. So first I create an array of zeros of the size I want to fill:

x = range(0,10,2)
y = range(0,10,2)
a = zeros(len(x),len(y))

so that will give me an array of zeros that will be 5X5. Now, I want to fill the array with a rather complicated function that I can't get to work with grids. My problem is that I'd like to iterate as:

for i in xrange(0,10,2):
    for j in xrange(0,10,2):
          .........
    "do function and fill the array corresponding to (i,j)"

however, right now what I would like to be a[2,10] is a function of 2 and 10 but instead the index for a function of 2 and 10 would be a[1,4] or whatever.

Again, maybe this is elementary, I've gone over the docs and find myself at a loss.

EDIT: In the end I vectorized as much as possible and wrote the simulation loops that I could not in Cython. Further I used Joblib to Parallelize the operation. I stored the results in a list because an array was not filling right when running in Parallel. I then used Itertools to split the list into individual results and Pandas to organize the results. Thank you for all the help

share|improve this question

2 Answers 2

up vote 2 down vote accepted

Some tips for your to get the things done keeping a good performance:

- avoid Python `for` loops
- create a function that can deal with vectorized inputs

Example:

def f(xs, ys)
    return x**2 + y**2 + x*y

where you can pass xs and ys as arrays and the operation will be done element-wise:

xs = np.random.random((100,200))
ys = np.random.random((100,200))

f(xs,ys) 

You should read more about numpy broadcasting to get a better understanding about how the arrays's operations work. This will help you to design a function that can handle properly the arrays.

share|improve this answer
    
Thanks! I took a look at broadcasting and I am trying to make the function vectorized, I agree that it would improve performance markedly. But part of the function is iterative in nature and that makes it hard for me to vectorize. –  irie_pandas Aug 8 '13 at 22:21
    
It would be nice then if you could give more details about your function, maybe you can get a better support..., also, take a look at Cython, which can significantly improve required for loops... –  Saullo Castro Aug 8 '13 at 22:22

First, you lack some parenthesis with zeros, the first argument should be a tuple :

a = zeros((len(x),len(y)))

Then, the corresponding indices for your table are i/2 and j/2 :

for i in xrange(0,10,2):
    for j in xrange(0,10,2):
        # do function and fill the array corresponding to (i,j)
        a[i/2, j/2] = 1

But I second Saullo Castro, you should try to vectorize your computations.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.