Home > Blockchain >  Enumerating regions of a two-dimensional array and interacting with them
Enumerating regions of a two-dimensional array and interacting with them

Time:05-26

I have two two-dimensional arrays res1, res2 of size 1501x780. I have to take slices from them, process these slices with a function and add the result to the array. The slice on res1 starts at (0,0) and goes up to (100,100). (size 100x100) It is fixed and does not move.

The second slice of the res2 array also starts at (0,0) and ends at (100,100). We got 2 slices and calculated the value using a certain formula, and added this value to the resulting array.

Then, with a fixed slice of res1, we change the value of res2 to (0,1),(100,101). That is, we shift the slice to the right by one pixel, leaving it at the same height. We do all the same manipulations. Then again we shift to the right by (0,2),(100,102). And so 100 elements. That is, up to (0,100)(100,200). Then we move it down one pixel, by (1.0)(0.100). And we also run from (1,0...100) to (1,0...200) Shift by (2,0),(2,100) and so on until (100,100),(200,200). slice of res1 remains fixed in all this. As a result, the resulting two-dimensional array of elements should be obtained ...

Thank you very much Robin De Schepper for his help, I understood how to interact with slices, but I didn’t describe my task in such a way. And if you add a second cycle for j in range(100) to its function, then I don't see how I can run through the slice values I need., and how to enter the result of the calculation into a two-dimensional array, because the declaration is ahead of results = [[]] doesn't work the way I imagined.

For some reason, while I was describing my question and thinking in parallel, I came to the conclusion that this is not possible to implement.

enter image description here

CodePudding user response:

To obtain an (x, y) matrix of results by performing an operation on (size_x, size_y) slices of input arrays res1 and res2, you can use a nested for loop and numpy:

import numpy as np

def operation(a, b):
  return a   b

res1 = np.array(res1)
res2 = np.array(res2)
x = 100
y = 100
size_x = 100
size_y = 100
start_x = 0
start_y = 0
results = np.empty((x, y))
for i in range(x):
 for j in range(y):
  slice1 = res1[(start_x   i):(start_x   i   size_x), (start_y   j):(start_y   j   size_y)]
  slice2 = res2[(start_x   i):(start_x   i   size_x), (start_y   j):(start_y   j   size_y)]
  results[i, j] = operation(slice1, slice2)

There are probably smarter numpy ways of doing this

  • Related