Is there a simple solution to substitute two NumPy
subarrays with a single one while the entires are a result of functions being called on the entries of the two former arrays.
For example:
[1, a, b][1, c, d] -> [1, f_add, f_sub]
with:
f_add(a, b, c, d):
return a + b + c + d
f_sub(b, d):
return b-d
Concrete:
[1, 0, 50], [1, 3, 1] -> [1, 54, 49]
As an addition, the row [1, 0, 50], [1, 3, 1]
is part of a bigger array (1st row in example), so it should be substituted in-place
([[1, 0, 50], [2, 0, 50], [1, 3.0, 1.0]],
[[1, 0, 50], [2, 0, 50], [2, 3.0, 1.0]])
([[1, 54, 49], [2, 0, 50]],
[[1, 0, 50], [2, 0, 50], [2, 3.0, 1.0]])
Thanks!
EDIT:
The functions f_add
and f_sub
are just examples to illustrate what I want to do and that the change in entries is a result of functions being called.
In reality I use slightly more complex functions that carry out a (more) meaningful computation.
The second thing is that this substitution should only be carried out on elements where the first entry is the same.
So in the first row only [1, 0. 50.]
and [1, 3.0, 1.0]
merge, while in the second it would be [2, 0., 50.]
and [2, 30, 1.0)
.
In this example, I first wanted to separate the issue of determining which sub-arrays are intended to merge by comparing the indices, but I guess it should be included to be as general as possible.
A more complete example result to the one above would be as follows then:
([[1, 0., 50.], [2, 0., 50], [1, 3.0, 1.0]],
[[1, 0., 50.], [2, 0., 50.], [2, 3.0, 1.0]])
leading to:
([[1, 54., 49.], [2, 0., 50.]],
[[1, 0., 50.], [2, 54., 49.]])