简体   繁体   中英

Find index of max value in each row of 2D array

I have the following code:

amplitude=[]
for i in range (0,160):
    amplitude.append(i+1)

#print(amplitude)

#split arrays up into a line for each sample
traceno=10                  #number of traces in file
samplesno=16             #number of samples in each trace. This wont change.

amplitude_split=np.array(amplitude, dtype=np.int).reshape((traceno,samplesno))
print(amplitude_split)

#print the maximum value of array along axis=1
max_amp=np.amax(amplitude_split,1)
print(max_amp)

#print the  indices of the maximum values along axis=1
ind_max_amp=np.argmax(amplitude_split, axis=1, out=None)
print(ind_max_amp)

I would like to find the indices for the max value in each row of the array. I am currently only getting ind_max_amp = [15 15 15 15 15 15 15 15 15 15] which I presume is the column of each max value.

What I need is tuples: (0,15) (1,15) etc...

Also, could anyone help me finding the index of 90% and 10% maximum in each row?

nb this is just test code and the max of my real data won't all be in the final column

You can get the maximum value for each row via np.amax using axis=1 and keepdims to keep each maximum value in a separate row, and then use np.argwhere to find the index of that maximum value in each row.

This will give you a list of indices tuples you want:

ind = np.argwhere(amplitude_split==np.amax(amplitude_split,1, keepdims=True))
list(map(tuple, ind))

output:

[(0, 15), (1, 15), (2, 15), (3, 15), (4, 15), (5, 15), (6, 15), (7, 15), (8, 15), (9, 15)]

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