I am not quite sure how to describe my question, but I will try.
I want to know if numpy has the functionality to do this:
Lets say I have a 2D array called grid:
grid = [ [0,0],
[0,0] ]
I also have a second 2D array called aList:
aList = [ [1,2],
[3,4] ]
I want to apply math to the first array based on the index of the first array.
So the math done at each iteration would look like this:
grid[i][j] = [(i - aList[k][0]) + (j - aList[k][1])]
Currently doing this in python with for loops is way to expensive so I need an alternative.
EDIT: more clarification, if I were not to use numpy I would write something like this:
for i in range(2):
for j in range(2):
num = 0
for k in range(2):
num += (i-aList[k][0]) + (j-aList[k][1])
grid[i][j] = num
This is however way to slow in python for the amount of data I have.
Your code can be reproduced and substantially sped up as follows:
i_s = np.arange(2)
j_s = np.arange(2)
fast_grid = (i_s + j_s[:, None])*len(aList) - aList.sum()
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.