Python Forum
Finding all maximum values in a matrix
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Finding all maximum values in a matrix
Hi there,

I am trying to compute all of the maximum values in a matrix using a "for loop" and needed some assistance. How would you go about doing this? I know that you would need to use the built-in max function, but I am unsure of how to use it inside a loop.

What are all of the maximum values? Do you mean maximum value of each row? In that case iterate over each row of matrix and call max() function, as you have already found yourself. If you need help with code, show us what you have done so far (in Python code tags) and tell what the problem is.
Thank you very much for your reply.

Yes, something like that, except that I am trying to find maximum values over every cell (or grid) within the matrix (called variable "Quantity") for every year for 40 years. This is what I have so far:


for i in Quantity:


I receive an error when running that, stating: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all(). I guess my code is erroneous?
In addition to the above posting, I receive the following error when I run that code:

"The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()"

What could I be doing wrong?

Any help would be greatly appreciated.

To be clearer, the variable "Quantity" is a 3D matrix that is composed of latitude and longitude (both make up latitude and longitude grid cells) and time (expressed in days).

What I would like to do is compute all maximum values by looping all latitudes, all longitudes, and then loop all days in the period, so that I could compute all maximum values over every grid cell (i.e. composed of latitude and longitude) for every day available, beginning with day 0.

My question is how should I construct a loop to do this?

Please ignore the "counts" in my code two posts above since they are not relevant in this case.

Once again, I would immensely appreciate any assistance.

Possibly Related Threads…
Thread Author Replies Views Last Post
  How to multiply a matrix with herself, until the zero matrix results peanutbutterandjelly 3 442 May-03-2021, 06:30 AM
Last Post: Gribouillis
  finding element of specific field in pandas adjacency matrix amjass12 0 335 Oct-27-2020, 09:27 AM
Last Post: amjass12
  Finding Max and Min Values Associated with Unique Identifiers in Python ubk046 1 517 May-08-2020, 12:04 PM
Last Post: anbu23
  Finding and indexing maximum value BlueBall 2 951 Oct-25-2018, 09:14 AM
Last Post: perfringo
  finding 2 max values in an array in python Akankshha 11 3,435 Oct-18-2018, 09:16 AM
Last Post: perfringo
  Need help for finding cumulative values in a loop Lightning1800 10 3,039 May-23-2018, 07:48 PM
Last Post: Lightning1800
  Finding values to draw a line on a curve kesenthilkumar 2 1,891 Sep-19-2017, 09:50 AM
Last Post: kesenthilkumar
  matrix from matrix python numpy array shei7141 1 2,142 Jan-16-2017, 06:10 PM
Last Post: micseydel

Forum Jump:

User Panel Messages

Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020