Find the distance from one point in a matrix to all other points in a matrix

后端 未结 3 1614
南笙
南笙 2020-12-03 20:18

I have a matrix a and I want to calculate the distance from one point to all other points. So really the outcome matrix should have a zero (at

3条回答
  •  盖世英雄少女心
    2020-12-03 21:18

    IMPORTANT: data_matrix is D X N, where D is number of dimensions and N is number of data points!

    final_dist_pairs=data_matrix'*data_matrix;

    norms = diag(final_dist_pairs);

    final_dist_pairs = bsxfun(@plus, norms, norms') - 2 * final_dist_pairs; Hope it helps!

    % Another important thing, Never use pdist function of MATLAB. It is a sequential evaluation, that is something like for loops and takes a lot of time, maybe in O(N^2)

提交回复
热议问题