Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Search for a specific row and remove from the array in Python

I have an array A. I want to seek rows with all zeroes and then remove them. Here I want to remove A[:,2] and append the row index to B. The desired output is attached.

import numpy as np

A=np.array([[ 0.00000000e+00,  3.57765318e-08,  0.00000000e+00,
         1.74215085e-07,  0.00000000e+00,  0.00000000e+00,
         0.00000000e+00],
       [-1.06733099e-07,  0.00000000e+00,  0.00000000e+00,
         0.00000000e+00,  3.50573100e-08,  0.00000000e+00,
         0.00000000e+00],
       [ 0.00000000e+00,  0.00000000e+00,  0.00000000e+00,
         0.00000000e+00,  0.00000000e+00,  0.00000000e+00,
         0.00000000e+00],
       [-1.78530448e-07,  0.00000000e+00,  0.00000000e+00,
         0.00000000e+00,  1.61650808e-08,  9.06895783e-08,
         0.00000000e+00],
       [ 0.00000000e+00, -4.46583743e-08,  0.00000000e+00,
        -5.99482549e-08,  0.00000000e+00,  0.00000000e+00,
         1.04606629e-07],
       [ 0.00000000e+00,  0.00000000e+00,  0.00000000e+00,
        -9.64183682e-08,  0.00000000e+00,  0.00000000e+00,
         9.64183682e-08],
       [ 0.00000000e+00,  0.00000000e+00,  0.00000000e+00,
         0.00000000e+00, -1.25633650e-07, -4.03926936e-07,
         0.00000000e+00]])

B=[5,8]

The desired output is

array([[ 0.00000000e+00,  3.57765318e-08,  0.00000000e+00,
         1.74215085e-07,  0.00000000e+00,  0.00000000e+00,
         0.00000000e+00],
       [-1.06733099e-07,  0.00000000e+00,  0.00000000e+00,
         0.00000000e+00,  3.50573100e-08,  0.00000000e+00,
         0.00000000e+00],
       [-1.78530448e-07,  0.00000000e+00,  0.00000000e+00,
         0.00000000e+00,  1.61650808e-08,  9.06895783e-08,
         0.00000000e+00],
       [ 0.00000000e+00, -4.46583743e-08,  0.00000000e+00,
        -5.99482549e-08,  0.00000000e+00,  0.00000000e+00,
         1.04606629e-07],
       [ 0.00000000e+00,  0.00000000e+00,  0.00000000e+00,
        -9.64183682e-08,  0.00000000e+00,  0.00000000e+00,
         9.64183682e-08],
       [ 0.00000000e+00,  0.00000000e+00,  0.00000000e+00,
         0.00000000e+00, -1.25633650e-07, -4.03926936e-07,
         0.00000000e+00]])
B=[2,5,8]

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You can use:

# find all rows where all values are 0
x = np.where((A==0).all(1))[0]

# delete them from A
A = np.delete(A, x, axis=0)

# prepend them in B
B = np.r_[x, B]

Alternative:

mask = (A==0).all(1)
x = np.where(mask)[0]

A = A[~mask]

B = np.r_[x, B]

output:

# A
array([[ 0.00000000e+00,  3.57765318e-08,  0.00000000e+00,
         1.74215085e-07,  0.00000000e+00,  0.00000000e+00,
         0.00000000e+00],
       [-1.06733099e-07,  0.00000000e+00,  0.00000000e+00,
         0.00000000e+00,  3.50573100e-08,  0.00000000e+00,
         0.00000000e+00],
       [-1.78530448e-07,  0.00000000e+00,  0.00000000e+00,
         0.00000000e+00,  1.61650808e-08,  9.06895783e-08,
         0.00000000e+00],
       [ 0.00000000e+00, -4.46583743e-08,  0.00000000e+00,
        -5.99482549e-08,  0.00000000e+00,  0.00000000e+00,
         1.04606629e-07],
       [ 0.00000000e+00,  0.00000000e+00,  0.00000000e+00,
        -9.64183682e-08,  0.00000000e+00,  0.00000000e+00,
         9.64183682e-08],
       [ 0.00000000e+00,  0.00000000e+00,  0.00000000e+00,
         0.00000000e+00, -1.25633650e-07, -4.03926936e-07,
         0.00000000e+00]])

# B
array([2, 5, 8])
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading