问题
As input I have 2D array PointXY[ ][ ] clusters, which looks like this:
[[23.237633,53.78671], [69.15293,17.138134], [23.558687,45.70517]] . . .
[[47.851738,16.525734], [47.802097,16.689285], [47.946404,16.732542]]
[[47.89601,16.638218], [47.833263,16.478987], [47.88203,16.45793]]
[[47.75438,16.549816], [47.915512,16.506475], [47.768547,16.67624]]
.
.
.
So elements in array are of type PointXY[ ], defined like this
public PointXY(float x, float y) {
this.x = x;
this.y = y;
}
what I would like to do is sort input clusters and write sorted to array PointXY[ ][ ] clustersSorted so that each PointXY (exept first row) in clusters gets compared to every value of first row. In other words the elements from the blue set on picture below get compared to every value circled by red. So I would like to compare each value from 2. Row onwards to every value in first row.
Comparing is done by calling Euclidian function.
public double euclidian(PointXY other) {
return Math.sqrt(Math.pow(this.x - other.x, 2)
+ Math.pow(this.y - other.y, 2));
}
Output should be of the same type 2D array but under each red circled point ( which stays the same in the same place in output array) should be points from blue part that are closest (by euclidean distance) to the red circled value.
So it's a data structure for K-Means clustering, so that each cluster is a column (circled green) and the first point is a center(circled red) of cluster and all the other points(circled in yellow) in column are points assigned to center.
So the question is how to iterate over input array clusters, compare values as described, and write them into array clustersSorted.
EDIT: I would like to calculate euclidean distance between each point in blue circled set with every value circled in red. And then sort them based on minimal euclidean distance. So in output array clustersSorted each point from blue circled set would be under closest point circled in red.
来源:https://stackoverflow.com/questions/62436112/sorting-2d-array-in-new-2d-array-k-menas-clustering-java