-1

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.880455499999997   0.868436999999998   0.853787499999999   0.836066499999999   0.815514999999999   0.785924499999999   0.661612000000002   0.208405500000000   0.0495730000000000  0.0148525000000000  0.00604500000000001 0.00292150000000000 0.00150100000000000 0.000730999999999999    0.000431999999999999    0.000222999999999999]

and

B=[0.915971250000000    0.909765000000000   0.902468749999999   0.894108749999999   0.883719999999998   0.871347499999999   0.857477500000000   0.841131250000000   0.821846250000000   0.796526250000000   0.673128750000000   0.208027500000000   0.0520962500000000  0.0187462500000000  0.00634375000000000 0.00295500000000000 0.00134500000000000 0.000226250000000000    0.000150000000000000    0.000113750000000000]

Could you help me to calculate it in matlab? The result shows 1 if they are similar, otherwise, 0 is different. enter image description here Thank in advance.

john2182
  • 117
  • 3
  • 11

1 Answers1

3

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.

Tibor Takács
  • 3,535
  • 1
  • 20
  • 23