Euclidean distance between two vectors (single row matrix)

I have two vectors (single row matrices). Assume that we already know the length len.

A = [ x1 x2 x3 x4 x5 .... ]
B = [ y1 y2 y3 y4 y5 .... ]

To calculate Euclidean distance between them what is the fastest method. My first attempt is:

diff = A - B
sum = 0
for column = 1:len
    sum += diff(1, column)^2
distance = sqrt(sum)

I have loop through this methods millions of times. So, I am looking for something which is fast and correct. Note that I am not using MATLAB and don't have pdist2 API available.

Answers


diff = A - B;
distance = sqrt(diff * diff');

or

distance = norm(A - B);

[val idx]    =  sort(sum(abs(Ti-Qi)./(1+Ti+Qi)));   

or

[val idx]    =  sort(sqrt(sum((Ti-Qi).^2))); 

Val is the value and idx is the original index value of the column being sorted after applying Euclidean distance. (Matlab Code)


To add to @kol answer,

diff = A - B;
distance = sqrt(sum(diff * diff')) % sum of squared diff

or

distance = norm(A-B);

Need Your Help

What is `stringify_keys' in rails and how to solve it when this error comes

ruby-on-rails ruby ruby-on-rails-3 ruby-on-rails-3.2

In a partial file of my application I have the following code snippet for displaying user navigation(through Devise):-

A good tutorial for using AccountManager in Android?

java android oauth oauth-2.0 accountmanager

Does anyone know of any good tutorials for using AccountManager in android, basically what I'm trying to do is request a OAuth2 token to use with a google api, any suggestions would be appreciated :)