Total members 11838 |It is currently Thu Dec 03, 2020 12:54 pm Login / Join Codemiles

Java

C/C++

PHP

C#

HTML

CSS

ASP

Javascript

JQuery

AJAX

XSD

Python

Matlab

R Scripts

Weka





Three different vectorized functions for euclidean distance.
Orginal function :
matlab code
function  D=mydist ( X )

if isempty(X)
error('Input matrix is empty \n');

end

% Get the number of points .
[NumberOfPoints]=size(X,1);

if NumberOfPoints<2
error('Number of points should be more than one \n');

end
% Create zeros array of size = NumberOfPoints x NumberOfPoints
D=zeros(NumberOfPoints,NumberOfPoints);

for i=1:NumberOfPoints-1
for j=1: NumberOfPoints
if(i<j)

% symmetric matrix
D(i,j)= sqrt(sum((X(i,:)-X(j,:)).^2));
D(j,i)= D(i,j);
end
end
end


end


First vectorized function( one loop- only) :
matlab code
function  D=mydist3( X )

if isempty(X)
error('Input matrix is empty \n');

end

% Get the number of points .
[NumberOfPoints]=size(X,1);

if NumberOfPoints<2
error('Number of points should be more than one \n');

end
% Create zeros array of size = NumberOfPoints x NumberOfPoints
tic
D=zeros(NumberOfPoints,NumberOfPoints);

for i=1:NumberOfPoints

% symmetric matrix

D(i,:)= sqrt(sum(((X-repmat(X(i,:), NumberOfPoints, 1)).^2)'));

end
toc

end


Vectorized function version 2 ( No loops)
matlab code
function  D=mydist ( X )

if isempty(X)
error('Input matrix is empty \n');

end

% Get the number of points .
[NumberOfPoints dim]=size(X);

if NumberOfPoints<2
error('Number of points should be more than one \n');

end


Xa=reshape(X',1,NumberOfPoints*dim);
Xa=repmat(Xa,NumberOfPoints,1);
X=repmat(X,1,NumberOfPoints);
D=(X-Xa).^2;
D=reshape(D,NumberOfPoints,dim,NumberOfPoints);
D=(sum(D,2));
D=sqrt(reshape(D,NumberOfPoints,NumberOfPoints));

end



Version 3 - ( No loops, another math formula is used, fastest version)
matlab code
function  D=mydist4( X )

if isempty(X)
error('Input matrix is empty \n');

end

% Get the number of points .
[NumberOfPoints dim]=size(X);

if NumberOfPoints<2
error('Number of points should be more than one \n');

end



[n,m]=size(X);
tic
D = sqrt(sum(X.^2,2)*ones(1,n)+ones(n,1)*sum(X.^2,2)'-2*(X*X'));

toc


end




_________________
M. S. Rakha, Ph.D.
Queen's University
Canada


Author:
Mastermind
User avatar Posts: 2715
Have thanks: 74 time
Post new topic Reply to topic  [ 1 post ] 

  Related Posts  to : Vectorized Matlab function calculate Euclidean distance
 Calculate euclidean distances     -  
 Need help with JavaScript function calculate the total pric!     -  
 Method to Get distance between two points     -  
 run .exe program in matlab     -  
 Matlab SVM training problem     -  
 Matlab basics examples     -  
 MATLAB clear memory     -  
 help me How do I load image from my pc to matlab     -  
 read file in matlab     -  
 naive Bayes classifier in MATLAB     -  



Topic Tags

Matlab Algorithms
cron





Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
All copyrights reserved to codemiles.com 2007-2011
mileX v1.0 designed by codemiles team
Codemiles.com is a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for sites to earn advertising fees by advertising and linking to Amazon.com