changeset 2:be1f915bd52a

Use accumarray instead of cellfun; seems faster
author Jordi Gutiérrez Hermoso <jordigh@octave.org>
date Tue, 06 Dec 2011 03:21:58 -0500
parents 90d2a292663c
children 069653867b3b
files computeCentroids.m
diffstat 1 files changed, 3 insertions(+), 3 deletions(-) [+]
line wrap: on
line diff
--- a/computeCentroids.m
+++ b/computeCentroids.m
@@ -10,8 +10,8 @@
   ##   assigned to it.
   ##
 
-  centroids = cell2mat(cellfun(@(i) mean (X(idx == i, :)), 
-                               num2cell([1:K]'), "uniformoutput", false))
-
+  [m,n] = size(X);
+  centroids = accumarray([repmat(idx, n,1), repmat(1:n, m,1)(:)], X(:),
+                         [K, columns(X)]) ./ hist (idx, 1:K)';
 endfunction