# HG changeset patch # User Jordi GutiƩrrez Hermoso # Date 1323190172 18000 # Node ID 6d94b2bafcd124d86567c9290e4547b2cdb0c88d # Parent 87433ad16bbf8adda461b2964511466cd76569c8 Replace complicated memory-intensive operation with a faster loop diff --git a/findClosestCentroids.m b/findClosestCentroids.m --- a/findClosestCentroids.m +++ b/findClosestCentroids.m @@ -5,9 +5,17 @@ ## vector of centroid assignments (i.e. each entry in range [1..K]) ## - ## Using broadcasting (auto BSX) as available in Octave 3.5.0+ - d = sum ((permute (X, [1,3,2]) - permute (centroids, [3,1,2])).^2, 3); + d = zeros (rows (X), rows (centroids)); + + ## This loop can be avoided with greater memory usage, but it doesn't + ## seem to speed things up. + for i = 1:rows (centroids) + + ## Using broadcasting (auto BSX) as available in Octave 3.5.0+ + d(:, i) = sum ((X - centroids(i,:)).^2, 2); + + endfor + [~, idx] = min (d, [], 2); endfunction -