-2

If I have an array in python:

a = np.array([20, 21, 19, 85, 25, 31, 21, 99, 3])

and I want to find the max value between indices 2 and 5 to return 85. How can I do this?

I know a.max() will output the value 99, but I am not sure how to specify the range.

sci-guy
  • 2,394
  • 4
  • 25
  • 46

2 Answers2

3

Just use slicing.

a = np.array([20, 21, 19, 85, 25, 31, 21, 99, 3])
a[2:5].max()

gives 85 as max value.

error
  • 2,356
  • 3
  • 23
  • 25
1

Simply call a[2:5].max().

It will search in the sub array containing the elements you're interested in.