File Exchange

image thumbnail

Euclidean Distance

version 1.0.0.0 (1.32 KB) by Ashok Kumar Pant
Calculates euclidean distance between two vectors.

3 Downloads

Updated 31 Dec 2013

View License

'z=mydist(w,p)' calculates euclidean distance between two vectors w:SxR and p:RxQ and returns z:SxQ,distances between w's rows and p's columns.

This function serve same as Matlab 'dist(w,p)' but requires far less memory to compute.

It can help to overcome 'Out of memory error' in larger data set training in neural network.

Cite As

Ashok Kumar Pant (2020). Euclidean Distance (https://www.mathworks.com/matlabcentral/fileexchange/44870-euclidean-distance), MATLAB Central File Exchange. Retrieved .

Comments and Ratings (2)

Ashok Kumar Pant

Thank you Jan, for the optimization.

Jan

This slightly modified version is 30% faster for 800x800 matrices:

function z = mydist(w,p)
S = size(w, 1);
Q = size(p, 2);
z = zeros(S, Q);
for i = 1:S
ww = w(i, :) .';
for j = 1:Q
v = ww - p(:, j);
z(i, j) = v' * v;
end
end
z = sqrt(z);

MATLAB Release Compatibility
Created with R2012b
Compatible with any release
Platform Compatibility
Windows macOS Linux

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!