performance - Remove for loop from clustering algorithm in MATLAB -
i trying improve performance of optics clustering algorithm. implementation i've found in open source makes use of loop each sample , can run hours...
i believe use of repmat() function may aid in improving performance when system has enough amount of ram. more welcome suggest other ways of improving implementation.
here code:
x data: [mxn] array m sample size , n feature dimensionality, of time greater one.
[m,n] = size(x); = 1:m d(i,:) = sum(((repmat(x(i,:),m,1)-x).^2),2).'; end
many thanks.
with enough ram play with, can use few approaches here.
approach #1: bsxfun
& permute
-
d = squeeze(sum(bsxfun(@minus,permute(x,[3 2 1]),x).^2,2))
approach #2: pdist
& squareform
-
d = squareform(pdist(x).^2)
approach #3 matrix-multiplication based euclidean distance calculations
-
xt = x.'; %//' [m,n] = size(x); d = [x.^2 ones(size(x)) -2*x ]*[ones(size(xt)) ; xt.^2 ; xt]; d(1:m+1:end) = 0;
for performance, bet on approach #3!
Comments
Post a Comment