I have a NumPy Array with size say 3*10
, I would like to extract sub rows with varying sizes from each row. The sub rows are centered in the middle pixel with varying pixel sizes. Then I take the average number of each subrow. I have a pseudo example below:
import numpy as np
arr = np.arange(1,31).reshape((3,10))
pixel_size = np.array([2,3,1])
## the subrow centers in the middle of the array, index 5
mask = [[5-2:5+2],[5-3:5+3],[5-1:5+1]] ## index for each row
### submatrix = arr[;,mask]
submatrix = [[3,4,5,6],[12,13,14,15,16,17],[24,25]]
## output = np.mean(submatrix, axis=1) output is the average number of each row in the submatrix
output = [4.5,14.5,24.5]
If I have over 10 millions of rows, how can I handle this situation fast.