title: NumPy: Average overlapping matricies?
body: <p>Suppose you have a matrix of shape <code>(n, m)</code>.</p>
  
  <p>Further, you have <code>k</code> smaller matrices of shape <code>(s, m)</code>.</p>
  
  <p>These <code>k</code> matrices might have been produced by sharding the larger matrix with something like:</p>
  
  <pre><code>stride = z
  ranges = [] # will contain sub lists of start / end positions
  for i in range(0, n, stride):
      if i + s &gt; n:
          ranges.append([n-s, n]) #&lt;-- if not evenly divisible include last ragged bit
          break
      else:
          ranges.append([i, i+s])
  
  # k = len(ranges)
  
  for a, b in ranges:
      submat = mat[a:b] # &lt;--- produces submats of shape (s, m)
      # not necessarily where submats come from, just for 
      # simple example purpose, feel free to add random noise to each submat
  
  </code></pre>
  
  <p>How would one, in numpy, join these <code>k</code> overlapping submatrices, averaging the overlapping regions?</p>
  
  <p>The goal is to then take these submats and reform the original mat, e.g. something like:</p>
  
  <pre><code>blank = np.zeros((n, m))
  for i in range(len(submats)):
      a, b = ranges[i]
      blank[a:b] += submats[i] #&lt;--- doesn't account for different amounts of overlapping regions
  </code></pre>
  
  <p>concrete numbers:</p>
  
  <pre><code>n = 693
  m = 10
  # so mat has shape (693, 10)
  
  s = 500
  stride = 50
  
  ranges = [[0, 500], [50, 550], [100, 600], [150, 650], [193, 693]]
  # notice that the range (0,50) doesn't need to be averaged
  
  k = 5 # len(ranges)
  
  # so we have k submats of shape (500, 10)
  </code></pre>
  
  <p>I currently am doing this:</p>
  
  <pre><code>def count_overlap(max_len, ranges): # from example 693, and [[0, 500], ...]
      tally = np.zeros(max_len)
      for i in range(max_len):
          for a, b in ranges:
              if a &lt;= i and i &lt; b:
                  tally[i] += 1
      return tally
  
  
  olap = count_overlap(693, ranges)
  olap[:55]
  # ([1., 1., ..., 1., 2., 2., 2., 2., 2.])
  olap[-50:]
  # ([2., 2., 2., 2., 2., 2., 2., 2., 1., 1., ..., 1., 1., 1.])
  </code></pre>
  
  <p>to know how much to divide each index of the mat</p>