Finding the mean value of subsets of an array

Could you do something like (pseudocode):

... 
while !eof(vid)
   ...
  for (i, row) in enumerate(rows)
    row_range = row .+ (-div(m, 2):div(m, 2))  # assuming m is even; will need to be tweaked if it's odd
    for (j, col) in enumerate(cols)
      col_range = col .+ (-div(n, 2):div(n, 2))
      sub_region = @view frame[row_range, col_range]
      data[i, j, k] = mean(sub_region)
    end
  end
end

That probably isn’t quite right, but hopefully it helps make a couple of useful points:

  1. Use @view or view to efficiently refer to a sub-part of an array without allocating a brand new copy of that part of the array
  2. Rather than constructing the array of sub-arrays, just compute the mean of each sub-array as you go. There’s no need to store them all at once, so you can make your code simpler and faster by operating on one sub-array at a time.
1 Like