Home > Software engineering >  Iterating through an 2D array with python
Iterating through an 2D array with python

Time:11-07

I want to store points in an array, but I am not sure how to iterate thorugh it.

x = np.dot(weights, corners)
x_points = np.zeros([100, 2])
for i in range(101):
    x_points = (x   corners[np.random.randint(3)]) / 2
    x = x_points

I have tried to iterate my array like this:

x = np.dot(weights, corners)
x_points = np.zeros([100, 2])
for i in range(101):
    x_points[i:,] = (x   corners[np.random.randint(3)]) / 2
    x = x_points

How do i store each point in my array?

CodePudding user response:

You for loop is meant to iterate over your number of points, but you never specifically pick the i-th row from the original x array to work on; instead, you just use the entire array on every iteration. Here's what you should be doing:

for i in range(101):
    x_points[i,:] = (x[i,:]   ...) / 2

(Note that you could even omit the second index if you wanted to: x[i] is the same as x[i,:].)

In addition, you indexing for storing in the x_points array is wrong. I'm assuming you meant to write x_points[i,:] like I did above, instead of x_points[i:,]. The latter slices the array from the i-th row to the end along the first axis, so your code would actually modify more than just one row of x_points at a time.

Finally, I don't understand why you are assigning x = x_points on every loop iteration. This way, after the loop runs for the first time, your original x array doesn't exist anymore. If you want to "rename" the finalized x_points, you need to place this line after the for loop.

CodePudding user response:

Using Numpy

Assuming you have only 10 points to keep it readable here, I have shown how to create the arrays using Numpy, and accessing its elements.

>>> weights = np.random.randn(10,4)
>>> corners = np.random.rand(4,2)
>>> x=np.dot(weights, corners)
>>> x
array([[ 0.41183813,  0.18830351],
       [ 0.10599174,  0.76246711],
       [ 0.50235149,  2.76642114],
       [ 0.17072047,  0.67126034],
       [-0.25400796, -1.20589651],
       [-0.04360992, -0.06102023],
       [ 0.0446144 ,  0.48355716],
       [ 0.8501312 ,  1.93899732],
       [ 0.44656254,  1.05180096],
       [ 0.00397146,  0.19248246]])
>>> x_points = np.zeros([10, 2])

Now you have a set of x_points, initialized to 0.0, and your dot product of weights and corners in x.

for i in range(len(x_points)):
    x_points[i] = (x[i]   corners[np.random.randint(3)]) / 2

All your randomly initialized points, that took x values, and the corners into consideration have been written to x_points.

>>> x_points
array([[ 0.33206919,  0.25078735],
       [ 0.17914599,  0.53786916],
       [ 0.46472124,  1.74498146],
       [ 0.29890573,  0.69740106],
       [-0.08596056, -0.17476289],
       [ 0.01923846,  0.39767525],
       [ 0.14845732,  0.39841418],
       [ 0.46610902,  1.39768402],
       [ 0.43682676,  0.88767137],
       [ 0.04302915,  0.52442659]])

For simple Python 2D arrays

You create a 2D array as follows:

matrix = [[0 for x in range(num_cols)] for y in range(num_rows)]

Then, you access it like you would access a list with the index:

matrix[2][3] = 23

for i in range(num_rows):
    for j in len(num_cols):
        print(f"The element in the {i}th row, {j}th col is {matrix[i][j]}")
  • Related