2

I have a 64x64 numpy array and I have a 5x64 window. I want to slide this window over the main numpy array with a step size of 1 and save the values that lie in that window in a column in an empty numpy array.

Thanks

motaha
  • 363
  • 2
  • 5
  • 19
  • what have you tried ? – Anum Sheraz Jul 19 '18 at 21:51
  • I tried creating a numpy array with 1s located in the corresponding locations in the window and the rest 0s. Then multiply this array with my main and remove all of the zeros from my output. The second iteration will be creating another 0 and 1 arrays with the 1s moved a row down and so on. The issue here is that I am creating the 0, and 1s array manually which isn' t ideal for large matrices. – motaha Jul 19 '18 at 21:54
  • I think i can just use the array index and put it in a loop. For example, arr[0:5], then arr[1:6] and so on. – motaha Jul 19 '18 at 22:04

1 Answers1

1

Exactly as you said in the comment, use the array index and incrementally iterate. Create a list (a in my case) to hold your segmented windows (window). In the end, use np.hstack to concatenate them.

import numpy as np 

yourArray = np.random.randn(64,64)        # just an example
winSize = 5

a = []                                    # a python list to hold the windows
for i in range(0, yourArray.shape[0]-winSize+1):
    window = yourArray[i:i+winSize,:].reshape((-1,1)) # each individual window
    a.append(window)

result = np.hstack(a)
F.S.
  • 1,175
  • 2
  • 14
  • 34