From 6c51a29ca219cb206107b4ed1db36e843e5100c9 Mon Sep 17 00:00:00 2001 From: neingeist Date: Mon, 17 Nov 2014 23:13:57 +0100 Subject: [PATCH] Compute centroid means (vectorized) --- ex7/computeCentroids.m | 14 +++----------- 1 file changed, 3 insertions(+), 11 deletions(-) diff --git a/ex7/computeCentroids.m b/ex7/computeCentroids.m index 2bd6095..5cadc20 100644 --- a/ex7/computeCentroids.m +++ b/ex7/computeCentroids.m @@ -27,17 +27,9 @@ centroids = zeros(K, n); % for k = 1:K - count = 0; - - % XXX vectorize - for i = 1:m - if idx(i) == k - centroids(k, :) += X(i, :); - count += 1; - end - end - if count > 0 - centroids(k, :) /= count; + X_in_k = X(idx == k, :); + if size(X_in_k, 1) > 0 + centroids(k, :) = sum(X_in_k)/size(X_in_k, 1); end end