Insert / Add / Update elements in the matrix

advertisements

Let's say I've initialized a matrix/array that has 400 rows, 3 columns:

distances = np.zeros([400, 3], dtype=np.float64)

Now, I have a for loop that returns 1200 objects (float values) and I want to "append" each element into distances (row by row) or assign those float values to each element in the matrix like:

distances[0,1] = item1,
distances[0,2] = item2,
distances[0,3] = item3,
distances[1,1] = item4,
distances[1,2] = .....

How can I do this? I tried numpy.append and numpy.insert but I failed. Any ideas?


Gather you items in a list, convert this list into NumPy array and reshape:

distances = np.array([item1, item2, ... item1200], dtype=float).reshape((400, 3))