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

### 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      endend`

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    tocend`

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'));   tocend`

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

 Tweet
Author:
 Posts: 2715Have thanks: 74 time
 Page 1 of 1 [ 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     -
naive Bayes classifier in MATLAB     -

### Topic Tags

Matlab Algorithms