How dist function works??

46 views (last 30 days)
Archana Goyal
Archana Goyal on 6 Mar 2015
Commented: Archana Goyal on 22 Mar 2015
If w=rand(4,3); p=rand(3,1);
z=dist(w,p)
ans is 0.4278 0.6786 0.3725 0.6324 how these values comes?
  1 Comment
Archana Goyal
Archana Goyal on 22 Mar 2015
Code for calculating Euclidean distance for music signals in matlab???

Sign in to comment.

Accepted Answer

Christiaan
Christiaan on 9 Mar 2015
Edited: Christiaan on 9 Mar 2015
The dist function is a 'Euclidean distance weight function' which applies weights to an input to get weighted inputs. At your example:
W is the (random) weight matrix. P is the input vector Z is the weighted input
If you type in the matlab prompt 'edit dist.apply' you find the formula behind this function. For your example, the weighted matrix is subtracted from the transposed and copied vector. Now it is squared and then the square root is taken. This is how the Euclidian norm is defined Norm = square((a-b)^2)
I have copied the code and made the example simpler to understand in the code below better:
clc;clear all;close all;
p=[1;2;3]
w=[1 1 2;1 1 1;1 2 1;1 1 1]
z1=dist(w,p)
% dist function
S = size(w,1);
Q = size(p,2);
z2 = zeros(S,Q);
if (Q<S)
p = p';
copies = zeros(1,S);
for q=1:Q
z2(:,q) = sum((w-p(q+copies,:)).^2,2);
end
else
w = w';
copies = zeros(1,Q);
for i=1:S
z2(i,:) = sum((w(:,i+copies)-p).^2,1);
end
end
z2 = sqrt(z2)
Here z1 and z2 should give the same answer.
I hope this makes it clearer.
Kind regards, Christiaan

More Answers (0)

Community Treasure Hunt

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

Start Hunting!