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

문제

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 ?

도움이 되었습니까?

해결책

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top