close

How to find maximum value in whole 2D array with indices

Hello Guys, How are you all? Hope You all Are Fine. Today We Are Going To learn about How to find maximum value in whole 2D array with indices in Python. So Here I am Explain to you all the possible Methods here.

Without wasting your time, Let’s start This Article.

Table of Contents

How to find maximum value in whole 2D array with indices?

  1. How to find maximum value in whole 2D array with indices?

    Here, You can test the max value using the index returned, indices returned should look like (array([0], dtype=int64), array([2], dtype=int64)) when you print the indices.

  2. find maximum value in whole 2D array with indices

    Here, You can test the max value using the index returned, indices returned should look like (array([0], dtype=int64), array([2], dtype=int64)) when you print the indices.

Method 1

which also elaborates how to find the max value and its (1D) index, you can use argmax()

>>> a = array([[10,50,30],[60,20,40]])
>>> maxindex = a.argmax()
>>> maxindex
3

You can then use unravel_index(a.argmax(), a.shape) to get the indices as a tuple:

>>> from numpy import unravel_index
>>> unravel_index(a.argmax(), a.shape)
(1, 0)

Method 2

Here, You can test the max value using the index returned, indices returned should look like (array([0], dtype=int64), array([2], dtype=int64)) when you print the indices.

import numpy as np
a = np.array([[200,300,400],[100,50,300]])
indices = np.where(a == a.max())
print(a[indices]) # prints [400]

# Index for max value found two times (two locations)
a = np.array([[200,400,400],[100,50,300]])
indices = np.where(a == a.max())
print(a[indices]) # prints [400 400] because two indices for max
#Now lets print the location (Index)
for index in indices:
    print(index)

Summery

It’s all About this issue. Hope all Methods helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which Method worked for you? Thank You.

Also, Read