changeset 6:6d94b2bafcd1 default tip

Replace complicated memory-intensive operation with a faster loop
author Jordi Gutiérrez Hermoso <jordigh@octave.org>
date Tue, 06 Dec 2011 11:49:32 -0500
parents 87433ad16bbf
children
files findClosestCentroids.m
diffstat 1 files changed, 11 insertions(+), 3 deletions(-) [+]
line wrap: on
line diff
--- 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
-