I am measuring the similarity of two data with same size is 20. That is
A=[0.915450999999999 0.908220499999997 0.900374999999996 0.890547499999996 0.88
The best solution in MATLAB to calculate the distance between vectors is the pdist
method:
http://www.mathworks.com/help/stats/pdist.html
It can use several metrics and it is quite well optimized. In the documantation these metrics are described very well.
pdist
compares all rowvectors with all rowvectors in a matrix and returns all of these distances. For two vectors you have to put them in a matrix and you have to call pdist
method using this matrix as input argument:
% A and B are the vectors of your example
X = [A; B];
D = pdist(X, 'cosine'); % D = 1.0875e-005
If you call pdist
with a matrix with more lines the output will be a vector as well. For example:
% A and B are the vectors of your example
X = [A; A; B; B];
D = pdist(X, 'cosine');
% D = 1.0e-004 * [0 0.1087 0.1087 0.1087 0.1087 0.0000]
D(1)
is A
compared with A
(1st row with 2nd row).
D(2)
is A
compared with B
(1st row with 3rd row).
D(3)
is A
compared with B
(1st row with 4th row).
D(4)
is A
compared with B
(2nd row with 3rd row).
D(5)
is A
compared with B
(2nd row with 4th row).
D(6)
is B
compared with B
(3rd row with 4th row).
Few years ago we implemented a simulation environment where several vectors inherit from a virtual line-scan camera are compared, and we used this method. It works perfectly.
As suggested here, you could use cosine similarity, which finds the angle between two vectors. Similar vectors have a value close to 1 and dissimilar vectors have a value close to 0.
function d = cosSimilarity(u, v)
d = dot(u,v)/(norm(u)*norm(v));
end