The process to obtain the complement of an array shaped (x,2) by removing rows from an array shaped (y,2) is as follows:
Here is an example code implementation of the above process:
import numpy as np
def array_complement(original_array, rows_to_remove):
# Find the complement array shape
complement_shape = (original_array.shape[0] - rows_to_remove.shape[0], 2)
# Create the complement array
complement_array = np.zeros(complement_shape)
# Iterate over each row in the original array
for i in range(original_array.shape[0]):
row = original_array[i,:]
# Check if the row is in the rows to remove
if np.any(np.all(rows_to_remove == row, axis=1)):
continue
# Add the row to the complement array
complement_array[i,:] = row
# Remove any rows with zeros (empty rows)
complement_array = complement_array[~np.all(complement_array == 0, axis=1)]
return complement_array
This function takes the original array and an array of rows to remove as inputs and returns the complement of the original array. Note that the row comparison is done using the np.all
function with the axis=1
argument to compare all elements in the row. The resulting complement array is then cleaned up by removing any rows with zeros (empty rows).
Asked: 2021-08-06 11:00:00 +0000
Seen: 8 times
Last updated: Apr 23 '21