0

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]

4 Answers 4

1

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])
Sign up to request clarification or add additional context in comments.

Comments

0

IIUC, you can do something like:

# the index of the first matching row
x = np.all(A==0, axis=1).argmax()

A_bar = np.delete(A, x, axis=0)
B.append(x)

Comments

0

Here's a way:

C = np.all(A==0, axis=1)
D = np.arange(A.shape[0])[C]
A = A[~C, :]
print(A)
B.extend(D)
print(B)

Output:

[[ 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]]
[5, 8, 2]

Comments

0

You can do this as:

mask = (A == 0).all(1)
B = np.append(B, np.arange(A.shape[0])[mask])  # .tolist()
A = A[~mask]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.