i generate a set of unique coordinate combinations by using
axis_1 = np.arange(image.shape[0])
axis_1 = np.reshape(axis_1,(axis_1.shape[0],1))
axis_2 = np.arange(image.shape[1])
axis_2 = np.reshape(axis_2,(axis_2.shape[0],1))
coordinates = np.array(np.meshgrid(axis_1, axis_2)).T.reshape(-1,2)
I then check for some condition and if it is satisfied i want to delete the coordinates from the array. Something like
if image[coordinates[i,0], coordinates[i,1]] != 0:
remove coordinates i from coordinates
i tried the remove and delete commands but one doesnt work for arrays and the other simply just removes every instance where coordinates[i,0] and coordinates[i,1] appear, rather than the unique combination of both. thanks
CodePudding user response:
What do you mean by "delete the coordinates from the array"? If image is a numpy array you can not simply delete a value. Every position of the numpy array exists and has a value. One option can be to set the corresponding coordinates to numpy.nan
CodePudding user response:
You can use the numpy.delete function, but this function returns a new modified array, and does not modify the array in-place (which would be quite problematic, specially in a for loop).
So your code would look like that:
nb_rows_deleted = 0
for i in range(0, coordinates.shape[0]):
corrected_i = i - nb_rows_deleted
if image[coordinates[corrected_i, 0], coordinates[corrected_i, 1]] != 0:
coordinates = np.delete(coordinates, corrected_i, 0)
nb_rows_deleted = 1
The corrected_i takes into consideration that some rows have been deleted during your loop.
