如何在matlab的pdist中添加一个点积作为距离函数?

dy2hfwbg  于 2022-11-15  发布在  Matlab
关注(0)|答案(1)|浏览(141)

mat_vec=zeros(100,10000);
点积是dot(xi, xj)

distanceFunction = @(xi, xj)dot(xi, xj)
mat_dist=pdist(mat_vec, distanceFunction)

错误信息,如

distanceFunction =

  function_handle with value:

    @(xi,xj)dot(xi,xj)

Error using pdist
Error evaluating distance function '@(xi,xj)dot(xi,xj)'.

Error in Task1_lab404_02 (line 45)
mat_dist=pdist(mat_vec, distanceFunction)

Caused by:
    Error using dot
    A and B must be same size.
q0qdq0h2

q0qdq0h21#

来自pdist documentation(重点是我的):
距离函数的形式为
function D2 = distfun(ZI,ZJ)
哪里
ZI是包含单个观测的1 x n向量。
ZJ是包含多个观测值的m2×n矩阵。该函数必须接受具有任意数量观测值的矩阵ZJ
因此,您需要在第二个输入上定义函数矢量化。对于点阵产品来说,这很容易做到:

distanceFunction = @(xi, xj) xi*xj';

但是请注意,这不是一个真正的距离函数,因为对于复数输入,distanceFunction(a,b)不等于distanceFunction(b,a)pdist只会给出其中一个结果。

相关问题