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!
#12
Hi killerrex,

Sounds good, thanks!

One last question that came to mind, suppose that, adhering to what was done previously, I wanted to compute 5-day cumulative values (using np.sum) over groups of 5 consecutive days per year (for each of the 140 years for each grid cell). The idea would be to begin with day 0 to day 4 (so the first 5 days in year 0), and then move to the next group of 5 consecutive days, from day 1 to day 5, and then day 2 to day 6, and then day 3 to day 7, and then day 4 to day 8.....etc....all the way to the end of the 51,000 day period (140 years).

The end result would be the same as before, as the previously generated loop that contains the "np.max" function would derive the "annual" maximum 5 consecutive day values, so nothing to the previous code would need to be deleted, just expanded on a little.

Doing this would result in 361 groups of 5 consecutive days per year for 140 years. What would be the best way to group the days like that and determine 5-day maximum values for each group? What I did was create a new variable, called Quantity2, and defined it as:

Quantity2=np.zeros(Quantity.shape) #so there are all zeros in the output. 
I would have to create another loop in order to define the first set of 5 consecutive days (in year 0). One way to do it is use a for loop, such as:

for time in Quantity2
        Quantity2[time,...]
        fiveconsecutivedays=np.sum(Quantity[time-2:time+2,...], axis=0)
Quantity is the original 3D matrix. The "time-2" and "time+2" gives a range of days relative to a specified day. For example, at Day 2, the range would be Day 0 to Day 4. Using that approach, though, I cannot use Day 0 and Day 1, as well as the final two days in the period, since that would lead to a range that contains days that do not exist. For example, if you subtract 2 from Day 0 or Day 1, you will end up with a negative day. Likewise, you will end up with days that exceed 51100 for the final two days due to time+2. I'm just uncertain how to specify the conditions in the for loop and wanted to know of a way to use them for this case?

Does the above coding make sense to you?

Thanks, again.
Reply


Messages In This Thread
RE: Looping in a 3D matrix - desperate for assistance! - by Lightning1800 - May-18-2018, 07:58 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Need desperate help needHelpp 2 3,363 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