Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python: Cell arrays comparison using minus function

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.

like image 447
Xiong89 Avatar asked Apr 23 '26 01:04

Xiong89


1 Answers

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]
like image 72
Elias Dorneles Avatar answered Apr 25 '26 16:04

Elias Dorneles