/K-Means Clustering and PCA/Octave/computeCentroids.m
http://github.com/gafiatulin/ml-class · Objective C · 49 lines · 37 code · 12 blank · 0 comment · 6 complexity · d52fcedd3acce30ad488a197f1e0fa4d MD5 · raw file
- function centroids = computeCentroids(X, idx, K)
- %COMPUTECENTROIDS returs the new centroids by computing the means of the
- %data points assigned to each centroid.
- % centroids = COMPUTECENTROIDS(X, idx, K) returns the new centroids by
- % computing the means of the data points assigned to each centroid. It is
- % given a dataset X where each row is a single data point, a vector
- % idx of centroid assignments (i.e. each entry in range [1..K]) for each
- % example, and K, the number of centroids. You should return a matrix
- % centroids, where each row of centroids is the mean of the data points
- % assigned to it.
- %
- % Useful variables
- [m n] = size(X);
- % You need to return the following variables correctly.
- centroids = zeros(K, n);
- centroidsCount = zeros(K,1);
- % ====================== YOUR CODE HERE ======================
- % Instructions: Go over every centroid and compute mean of all points that
- % belong to it. Concretely, the row vector centroids(i, :)
- % should contain the mean of the data points assigned to
- % centroid i.
- %
- % Note: You can use a for-loop over the centroids to compute this.
- %
- for i =1: m
- for k =1:K
- if(idx(i) == k)
- centroids(k,:) = centroids(k,:) + X(i,:);
- centroidsCount(k) = centroidsCount(k) +1;
- end
- end
- end
- for k=1:K
- centroids(k,:) = centroids(k,:) ./ centroidsCount(k);
- end
- % =============================================================
- end