So let's say I have a numpy array like this:
import numpy as np
mat = np.array([[4, 8, 1], [5, 10, 6]])
print(np.argmax(mat)) # prints 4
print(np.argmax(mat, axis=1)) # prints [1 1], index of maximum values along the rows
Does Kotlin have a similar (built in) function? I found a Kotlin bindings for NumPy, but I didn't find the function implemented.
Thanks in advance!
Use withIndex()
and maxByOrNull()
:
fun <T : Comparable<T>> Iterable<T>.argmax(): Int? {
return withIndex().maxByOrNull { it.value }?.index
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With