I have a vector (V1) with which I need to calculate the similarity of other vectors (ex V2,V3 ... ) which may be of different lengths. The different angle here is that the elements inside the vectors are itself similar to each other.

V1 = c("a","b","c")
V2 = c("a","d","e","f","g")
V3 = c("b","c","f")

The elements are similar to each other for example:

      a      b        c       d       e       f      g
a   1.00    0.18    0.01    0.96    0.12    0.46    0.73
b           1.00    0.07    0.36    0.13    0.47    0.92
c                   1.00    0.88    0.62    0.65    0.31
d                           1.00    0.86    0.96    0.55
e                                   1.00    0.25    0.91
f                                           1.00    0.13
g                                                   1.00

The usual similarity methods like cosine similarity, correlation do not make use of the similarity between the elements of the vectors. Any suggestions to how should I calculate the vector similarity?? I am using R.

  • $\begingroup$ Immediately comes to mind. Similarity between abc and adefd would be mean(aa+ad+ae+af+ag + ba+bd+be+bf+bg + ca+cd+ce+cf+cg) with corresponding proximities from the table substituting these paiwise combinations. $\endgroup$
    – ttnphns
    Jun 2, 2014 at 11:47
  • $\begingroup$ Thanks of the response. This measure does not ensure that the highest similarity value will be when matching the same vector. For example : Matching V1 with V1 according to the similarity table given in the question the similarity score will be mean(aa + ab + ac + ba + bb + bc + ca + cb + cc) = 3.52/9 = 0.391 $\endgroup$
    – Tusheet
    Jun 3, 2014 at 4:11
  • $\begingroup$ where as comparing V1 (abc) to (dg) will be mean(ad + ag + bd+ bg+ cd+cg) = 4.16/6 = 0.693 $\endgroup$
    – Tusheet
    Jun 3, 2014 at 4:13
  • $\begingroup$ OK, here's an idea. Let's have abc vs badcf. Draw one string along the other until their match is maximized. Here, the maximized match will be 0/b a/a b/d c/c 0/f. In this superposition we have two pairs of identical characters; their "weight" is 4 characters in toto, and they, of course give similarity coefficient=1. Remove the pairs and leave b vs bdf. Compute similarity b/w these as in my 1st comment: mean(bb+bd+bf)=0.61; this coefficient is based on 4 characters in toto, so its weight is 4. Finally, average the two coefficients with weighting: (4*1+4*0.61)/(4+4)=0.805. $\endgroup$
    – ttnphns
    Jun 3, 2014 at 6:59

1 Answer 1


Use Dynamic time warping (Wikipedia).

It can align series of different length, as long as you can specify the distance of individual elements. It is common to use DTW with linear or quadratic distances, but in your case your element similarity matrix can take care of this.

I'm not using R. It is too slow for anything that cannot be vectorized.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.