简体   繁体   中英

Delete certain objects in 2d array

Hello i have a txt file that i want to make into an array. I want to remove the rows in my array that doesnt fill my conditions. For example Comlumn 0 must be between 10 and 60 Column 1 must be positiv Column 2 must be between 1 and 4

I have tryed finding a way to define the conditions, but whit no luck. I have written the folowing code:

RD = np.loadtxt(filename)
for i in range(len(RD)):
    if (RD[:,0] < 10 or RD[:,0] > 60):
         RD= np.delete(RD,[i,0])

    elif RD[:,2]<0:
        RD= np.delete(RD,[i,1])  

    elif (RD[:,2]<1 or RD[:,2]>4):
        RD= np.delete(RD,[i,2])
print(RD)

Can you help me define the conditions correctly?

You probably want to make a variable which is a set of row indexes which fail the condition, and then pass that variable as the argument through the function np.delete() at the end.

rows_to_del=[row for row in range(RD.shape[0]) 
             if (RD[row,0] < 10 or RD[row,0] > 60) 
             or RD[row,2] > 0 
             or (RD[row,2]<1 or RD[row,2]>4)]

RD = np.delete(RD, rows_to_del, axis=0)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM