CMeans
Executes one iteration of the C-means clustering algorithm. This function calculates the distance matrix, membership matrix, new centroids, cost values, and total cost function for a single iteration.Parameters
Array of points to cluster. Each point has
x and y coordinates.Array of initial centroids. Each centroid has
x and y coordinates.Returns
Returns an object containing the results of one C-means iteration:Matrix of distances where each element [i][j] is the distance between the i-th centroid and the j-th point.
Membership matrix where each element [i][j] is 1 if the j-th point belongs to the i-th centroid, otherwise 0.
New array of centroids calculated as the mean of all points belonging to each centroid.
Array of cost values for each centroid.
Total cost as the sum of all individual cost values.
Example
Helper Functions
getDistanceMatrix
Calculates the distance matrix between each point and each centroid.Array of points.
Array of centroids.
getMembershipMatrix
Generates the membership matrix from the distance matrix.Matrix of distances between points and centroids.
getNewCentroids
Calculates new centroids based on the membership matrix.Array of points.
Membership matrix.
getCostValues
Calculates the cost values for each centroid based on the membership and distance matrices.Membership matrix.
Distance matrix.
getCostFunction
Calculates the total cost from an array of individual cost values.Array of individual cost values.
euclidianDistance
Calculates the Euclidean distance between two points.First point.
Second point.
zeroMatrix
Creates a zero matrix with the same dimensions as the input matrix.Input matrix.
generateRandomPoints
Generates an array of random points.Number of points to generate.