0

I have 3-dim array for maximum temperature (if temperature >25, otherwise 0)

 heat_values[580,842,]
  [1]  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0    
 [16]  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0    
 [31]  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0 27.1 27.6    
 [46] 28.4  0.0 25.5 25.7 26.2 26.1  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0    
 [61]  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0    
 [76]  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0    
 [91]  0.0  0.0  0.0  0.0 26.7 26.1 27.8 29.1 30.5  0.0 26.1 27.7 30.1 31.3 31.6    
[106] 29.6 28.7 25.7  0.0  0.0  0.0 26.1 27.8  0.0  0.0 26.1 30.9 31.4 33.3 36.2    
[121] 28.7 25.3  0.0 25.4 26.2 29.5  0.0 27.6 31.8 31.6 27.9 25.1  0.0  0.0 26.9    
[136] 28.1 25.9  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0    
[151]  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0    
[166]  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0    
[181]  0.0  0.0  0.0  0.0

I want to get maximum value between two 0 and set the value at the max-value before the second 0. For example:

 heat_values[580,842,100:109]
 [1]  0.0 26.1 27.7 30.1 31.3 31.6 29.6 28.7 25.7  0.0

Max is 31.6, so I want that in the result value 31.6 at index 108 and so on. Expected output for above:

heat_values[580,842,100:109]
[1]  0.0 26.1 27.7 30.1 31.3 31.6 29.6 28.7 31.6  0.0

Update: Example input and expected output:

x <- c(0.0,0.0,27.1,27.6,22,
       0.0,25.5,25.7,26,21,
       0.0,25.4,26.2,29.5,
       0.0,0.0,27.6,31.8,31.6,27.9,25.1,
       0.0)

There are 4 groups, groups decided based on zeros around non-zero values. Expected output, get the max per group, and update last value in a group with max value. Expected output:

x <- c(
  0.0,0.0,27.1,27.6,27.6,
  #                 ^^^^
  # max is 27.6 so we update last value
  0.0,25.5,25.7,26,26,
  #                ^^
  # max is 26 so we update last value
  0.0,25.4,26.2,29.5,
  #             ^^^^
  # max is the same as last value 29.5
  0.0,0.0,27.6,31.8,31.6,27.9,31.8,
  #                           ^^^^
  # max is 31.8, update last value
  0.0)
zx8754
  • 52,746
  • 12
  • 114
  • 209

0 Answers0