I have the following xy
numpy array which represents the locations of the vertices of some triangles:
array([[[ 0.30539728, 49.82845203],
[ 0.67235022, 49.95042185],
[ 0.268982 , 49.95195348]],
[[ 0.268982 , 49.95195348],
[ 0.67235022, 49.95042185],
[ 0.27000135, 50.16334035]],
...
[[ 1.00647459, 50.25958169],
[ 0.79479121, 50.3010079 ],
[ 0.67235022, 49.95042185]],
[[ 0.79479121, 50.3010079 ],
[ 0.6886783 , 50.25867683],
[ 0.67235022, 49.95042185]]])
Here, it's an array of shape (10, 3, 2)
but it could as well be (5, 3, 2)
or (18, 3, 2)
, you name it. In any case it's of shape (N, 3, 2)
.
I have another numpy array to_replace
of shape (4, 2)
but it could as well be (6, 2)
or (7, 2)
, but always of shape (M, 2)
:
array([[ 1.08267406, 49.88690993],
[ 1.1028248 , 50.01440407],
[ 0.74114309, 49.73183549],
[ 1.08267406, 49.88690993]])
It represents the locations of pairs of coordinates that can be found in my first array. Note that each of these pairs is present at least once in xy
but could be present more than once.
Finally, I have a third array replace_by
of which shape (8,)
(or of shape (M*2)
based on the indication above) and which values are meant to replace exactly those contained in to_replace
in my first xy
array. It looks like this:
array([ 0.87751214, 49.91866589, 0.88758751, 49.98241296, 0.70674665, 49.84112867, 0.87751214, 49.91866589])
So basically all pairs [1.08267406, 49.88690993]
in xy
should be replaced by [0.87751214, 49.91866589]
for example.
My current code looks like this but it works only if to_replace
and replace_by
are strictly of shape (2, 2)
.
indices = (xy == to_replace[:, None][:, None])[0]
xy[indices] = replace_by
I already looked at a number of answers and actually got inspired by some of them but I still can't get it to work.