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]
CodePudding user response:
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])
CodePudding user response:
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)
CodePudding user response:
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]
CodePudding user response:
You can do this as:
mask = (A == 0).all(1)
B = np.append(B, np.arange(A.shape[0])[mask]) # .tolist()
A = A[~mask]