MATLAB: Hi, please I want to calculate the distance between this tow matrix using pdist2

MATLABmatrixpdist2

Best Answer

Olfa
p=[26 255 255 255 0 255 255]
s=[255 255 255 255 0 255 255]
% vector distance from p to s
s-p
=
229 0 0 0 0 0 0
the euclidean distance is the magnitude or norm of the vector distance
s_p_dist_euclid=pdist2(s,p,'euclidean')
=
229
the square euclidean distance is the area of the square with side the previous euclidean distance
s_p_dist_sqeuclid=pdist2(s,p,'squaredeuclidean')
=
52441
229*229
=
52441
citiblock distance
s_p_dist_cityblock=pdist2(s,p,'cityblock')
s_p_dist_cityblock =
229
minkowski default coeff is 2
>> s_p_dist_mink=pdist2(s,p,'minkowski')
s_p_dist_mink =
229
s_p_dist_mink=pdist2(s,p,'minkowski',1.5)
s_p_dist_mink =
2.289999999999999e+02
Cheby distance: maximum coordinate difference
s_p_dist_chevy=pdist2(s,p,'chebychev')
s_p_dist_chevy =
229
cosine distance: 1-cos(alpha) alpha is the angle between vectors s and p
s_p_dist_cos=pdist2(s,p,'cosine')
s_p_dist_cos =
0.069480529510180
correlation distance: 1-sample correlation, it means, the opposite of the correlation
s_p_dist_corr=pdist2(s,p,'correlation')
s_p_dist_corr =
0.307354858007336
the correlation distance of of same vectors would be null because it's max correlated
s_p_dist_corr=pdist2(p,p,'correlation')
=
2.220446049250313e-16
hamming distance: the percentage of coordinates that differ
s_p_dist_hamming=pdist2(s,p,'hamming')
=
0.142857142857143
only one coordinate different:
1/7 = 0.142857142857143
Jaccard distance: percentage of nonzero coordinates that differ
s_p_dist_jaccard=pdist2(s,p,'jaccard')
=
0.166666666666667
you also define a custom distance function
D = pdist2(X,Y,@distfun)
the custom distance function has to have this header
function D2 = distfun(ZI, ZJ)
If you find this answer of any help solving your question, please click on the thumbs-up vote link, or mark it as accepted answer
thanks in advance
John