Imagine two related problems:
I have one 2-dim data point and a set of $M$ 2-dim other data points. How to calculate sum of all distances between one point and those $M$ points? Result is one number.
Now I have $N$ 2-dim points and same set of $M$ 2-dim data points as above. How to calculate sums of all distances between $N$ points and those $M$ points? It should be equivalent to looping through $N$ points and getting those sums. Result is $N$ numbers.
This problem relates to clustering. I extracted clusters from calibration data with kmeans, but now I want to identify to which cluster my new point(s) belong to. Of course, simple looping is inefficient.
UPDATE:
This is an R question.
Mathematical formulation:
$(x,y)_i$ is $N$-set, $(X,Y)_k$ is $M$-set.
$$\sum_{k=1}^M (x_i - X_k)^2 + (y_i - Y_k)^2 = d^2_i$$
UPDATE2:
One of the methods I discovered is to split everything into two steps:
- (calibration) identify clusters with some method like stats::kmeans(). It provides classes for the entire dataset.
- (backtesting) split dataset into train- and test-subsamples and use class::knn(). Cluster-IDs from train-subsample are assumed to be 'TRUE'. At the output it will deliver cluster-IDs identified by knn within test-subsample. test cluster-IDs are chosen with Euclidean metrics, exactly as I need.
Although I do not have control over the process it still delivers satisfactory result and speed.
The realtime solution, where single point has to be classified, can be implemented with calibrated sample (after step 1).