I have 3 cell arrays with each cell array have different sizes of array. How can I perform minus function for each of the possible combinations of cell arrays?
For example:
import numpy as np
a=np.array([[np.array([[2,2,1,2]]),np.array([[1,3]])]])
b=np.array([[np.array([[4,2,1]])]])
c=np.array([[np.array([[1,2]]),np.array([[4,3]])]])
The possible combination here is a-b, a-c and b-c.
Let's say a - b:
a=2,2,1,2 and 1,3
b=4,2,1
The desired result come with shifting windows due to different size array:
(2,2,1)-(4,2,1) ----> -2,0,0
(2,1,2)-(4,2,1) ----> -2,-1,1
(1,3) -(4,2) ----> -3,1,1
(1,3) -(2,1) ----> 4,-1,2
I would like to know how to use python create shifting window that allow me to minus my cell arrays.
You can use the function sliding_window() from the toolz library to do the shifting window:
>>> import numpy as np
>>> import toolz
>>> a = np.array([2,2,1,2])
>>> b = np.array([4, 2, 1])
>>> for chunk in toolz.sliding_window(b.size, a):
...: print(chunk - b)
...:
[-2 0 0]
[-2 -1 1]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With