Python Forum
Looping in a 3D matrix - desperate for assistance!
Thread Rating:
  • 1 Vote(s) - 4 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Looping in a 3D matrix - desperate for assistance!
#5
Hi Killerrex,

Once again, a very big thank you for this response.

In my case, I am working with a 3D matrix, with the first dimension being time, the second being latitude (64 lines of latitude), and the third being longitude (128 degrees of longitude). The units of time are, thankfully, very standard and are not overly complex. The final code that you listed above is actually very relevant, since it defines how I would like to convert the default time units of days to years, and then using that to have Python compute the maximum values for every year for every grid cell (made up of latitude and longitude) in a loop.

Just a few additional questions surfaced:

1. If you wanted to use your last code to include every grid cell (made up of 64 degrees of latitude and 128 degrees of longitude), would that provide annual maximum values for each grid cell? If so, could you combine your last code with my existing code above:

Quantity=Q
 
        for lat in range(np.size(Quantity,axis=1)):
                for lon in range(np.size(Quantity,axis=2)):
                        Quantity[:,lat,lon]
With this:

year = np.arange(Quantity.shape[0]) // 365
for y in range(max(year) + 1):
    v = np.max(Quantity[year == y, ...])
    print(f"Max for year {y} = {v}")
For example, would that combination produce:

Grid cell #1 for year 0: Max value derived
Grid cell #2 for year 0: Max value derived
Grid cell #3 for year 0: Max value derived
....
Grid cell #1000 for year 0: Max value derived

Then, move on to year 1....

etc....all the way to year 139?

Does that make sense?

2. What does the "[0]" mean in the first line of your code?
3. What does the "+1" suggest? Does that allow the loop to move from one year to the next, all the way to year 139?
4. What does the "..." imply in line 3?
5. Could you elaborate a little more on what you have inserted in your print function, in line 4?

Many thanks, once again, and I apologize for the list of questions - I am fairly new to programming with loops!
Reply


Messages In This Thread
RE: Looping in a 3D matrix - desperate for assistance! - by Lightning1800 - May-15-2018, 08:34 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Need desperate help needHelpp 2 2,641 Aug-06-2017, 06:02 PM
Last Post: eyn2k

Forum Jump:

User Panel Messages

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