I just started to use Eigen Matrix algebra library and aim to create a similarity matrix of a dataset, suggestions?

StackOverflow https://stackoverflow.com/questions/18390425

Question

I try to create a similarity matrix with eigen library on a dataset. I just read the csv file into eigen matrix but know as a matlab customer I am looking for something like bsxfun or something to define the distances between instances by Euclidean distance calculation.How can I get away with a solution or what sources, functions might help me ?

Was it helpful?

Solution

Assuming your samples are stored row-wise in a matrix D, then you can do:

VectorXd N = D.rowwise().squaredNorm();
MatrixXd S = N.replicate(1,n) + N.transpose().replicate(n,1);
S.noalias() -= 2. * D * D.transpose();
S = S.array().sqrt();

This exploits the fact that |x-y|²=x²+y²-2x'y. The noalias() statement is just an optimization to Eigen there is no risk of aliasing in this product, thus no temporary is needed. The .array() statement switches to the array world where all functions are applied coefficient-wise.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top