I have this function here designed to create uniform particles over given x and y ranges, which are going to be 1x2 matrices. However, when I try and run it, I get the error below. I feel that there is a slicker way to assign the x and y values into my particles matrix. How can I solve this?
def create_uniform_particles(x_range, y_range, N):
particles = np.empty((N, 2))
new_x = uniform(x_range[0], x_range[1], size=(N,1))
new_y = uniform(y_range[0], y_range[1], size=(N,1))
for i in range(N):
particles[i][0] = new_x[i]
particles[i][1] = new_y[i]
return particles
#Error:
Traceback (most recent call last):
File "/Users/scottdayton/PycharmProjects/Uncertainty Research/particle.py", line 83, in <module>
particle_filter(init, sigma, obs, n, trans, sigma0)
File "/Users/scottdayton/PycharmProjects/Uncertainty Research/particle.py", line 49, in particle_filter
particles = create_uniform_particles(new_x_range, new_y_range, n)
File "/Users/scottdayton/PycharmProjects/Uncertainty Research/particle.py", line 8, in create_uniform_particles
new_x = uniform(x_range[0], x_range[1], size=(N,1))
IndexError: too many indices for array
Your code for this function appears to be correct (at least, it works for me without any modifications) when I do:
I recommend verifying that the variables in the function one level above create_uniform_particles (wherever you set up new_x_range and new_y_range) are the shapes you were expecting. Since this function you wrote works for inputs correctly passed in, it's probably happening somewhere around there.
In terms of assigning the x's and y's, you can use hstack to concatenate the new_x and new_y vectors together into an array. Give this below a try if you like it better. As a side note, the alternative to hstack is vstack, which will concatenate them after each other instead of "next to" each other in your case.