Vectorize finding center of sets of points in multidimensional array in Numpy

258 views Asked by At

I've got a multidimensional array that has 1 million sets of 3 points, each point being a coordinate specified by x and y. Calling this array pointVec, what I mean is

np.shape(pointVec) = (1000000,3,2)

I want to find the center of each of the set of 3 points. One obvious way is to iterate through all 1 million sets, finding the center of each set at each iteration. However, I have heard that vectorization is a strong-suit of Numpy's, so I'm trying to adapt it to this problem. Since this problem fits so intuitively with iteration, I don't have a grasp of how one might do it with vectorization, or if using vectorization would even be useful.

1

There are 1 answers

0
Ehsan On BEST ANSWER

It depends how you define a center of a three-point. However, if it is average coordinates, like @Quang mentioned in the comments, you can take the average along a specific axis in numpy:

pointVec.mean(1)

This will take the mean along axis=1 (which is second axis with 3 points) and return a (1000000,2) shaped array.