File Exchange

image thumbnail

Euclidean Distance

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


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 (, MATLAB Central File Exchange. Retrieved .

Comments and Ratings (2)

Ashok Kumar Pant

Thank you Jan, for the optimization.


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;
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!