Ask Your Question
1

What is the process to obtain the complement of an array shaped (x,2) by removing rows from an array shaped (y,2)?

asked 2021-08-06 11:00:00 +0000

lalupa gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
2

answered 2021-04-23 01:00:00 +0000

ladyg gravatar image

The process to obtain the complement of an array shaped (x,2) by removing rows from an array shaped (y,2) is as follows:

  1. Create a new array of shape (y-x,2) to hold the complement of the original array.
  2. Iterate over each row in the original array.
  3. Check if the row is present in the array shaped (x,2).
  4. If the row is not present in the original array, add it to the new complement array.
  5. Return the new complement array.

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).

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2021-08-06 11:00:00 +0000

Seen: 8 times

Last updated: Apr 23 '21