Frage

I am using pdist2(x(i), y(j), 'euclidean') formula to find euclidean distance between x and y instead of the manual method

sqrt((x(i)-y(i))^2).

And to find co relation coefficient I am using corrcoeff( x(i), y(j)) . Is this the correct way to find correlation coefficient and euclidean distance between x and y matrix? I get different answers when I use formula and manual method.

War es hilfreich?

Lösung 2

The correlation between the two matrices can be calculated as:

r = corr2(x,y)

Now, if you are after the element-wise distance, how about:

dist=gsqrt((x-y).^2);

Andere Tipps

I think it is not correct.

I suppose x and y are matrixes, so you are using pdist2 to compute the distance between the observation i (from x(i)) and j (from y(j)). In the case of the manual method you are using the i index in both of them. Maybe the differences are due to the incorrect use of the indexes i and j. Show us your code so we can confirm it.

By the way, as @Luis pointed out, it is better to use pdist2 to compute all the distances at the same time (it is much faster). So, if you have two matrixes x and y, use: pdist2(x,y).

The same for the correlation.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top