0

I want to use pdist() in MATLAB and use a custom function "Extended Jaccard" defined as bellow:

 S_EJ(X_a, X_b) = (X_a . X_b) / (||X_a||^2 +  ||X_b||^2 - X_a . X_b)

where X_a . X_b represents the inner product between vectors X_a and X_b and || ||^2 is the norm_2 of a given vector.

It is strongly recommended to take advantage of bsxfun() as it is based on multi-threading. Ofcourse, if this is possible.

What do you think about:

  SEJ(x,y) = bsxfun(@(x, y) (x.*y)./(norm(x)^2 + norm(y)^2 - (x.*y)) , x, y);

Thanks for your opinions in advance.

Ander Biguri
  • 35,140
  • 11
  • 74
  • 120
Yas
  • 811
  • 4
  • 11
  • 20
  • so is this home work for us or for you? At least show us what you have tried and done so far please. – GameOfThrows Mar 16 '16 at 12:35
  • GameOfThrowns, of course it is not a homework. My only doubt was to use bsxfun() in a proper way. The function would like this : S_EJ = @(X_a,X_b)(bsxfun(xxxxxx,X_a,X_b)) – Yas Mar 16 '16 at 12:44
  • I edited the question, please have a look :-) – Yas Mar 16 '16 at 12:50
  • Does the code you posted work? is it fast enough? what problem does that code has? – Ander Biguri Mar 16 '16 at 13:38

1 Answers1

0

Your function uses elementwise multiplication .* and division ./ which produces a vector output, not a number, which is expected. You want the dot product and regular division.

Assumming row vectors:

ej_similarity = (vec1*vec2')/(norm(vec1)^2 + norm(vec2)^2 - vec1*vec2'); 

Assuming column vectors:

ej_similarity = (vec1'*vec2)/(norm(vec1)^2 + norm(vec2)^2 - vec1'*vec2); 

Generalized but a bit slower:

ej_similarity = (dot(vec1, vec2)/(norm(vec1)^2 + norm(vec2)^2 - dot(vec1,vec2));