我有两个欧几里得距离函数,一个使用bsxfun,另一个使用repmat。在Matlab 2012a,OSX上,他们给出了略有不同的结果。例如
x = randn(32, 50);
y = randn(32, 50);
xx = sum(x.*x, 1);
yy = sum(y.*y, 1);
xy = x'*y;
d1 = sqrt(abs(repmat(xx', [1 size(yy, 2)]) + repmat(yy, [size(xx, 2) 1]) - 2*xy));
d2 = sqrt( abs(bsxfun(@plus, xx', bsxfun(@minus, yy, 2*xy)) ));
isequal(d1, d2)
figure;hist(d1(:)-d2(:), 50)提供:

为什么会这样呢,还是我漏掉了什么?
发布于 2016-08-04 04:51:48
您正在执行的操作顺序是不同的。将括号放在下面这样
d1 = sqrt(abs(repmat(xx', [1 size(yy, 2)]) + (repmat(yy, [size(xx, 2) 1]) - 2*xy)));你会得到同样的结果
https://stackoverflow.com/questions/38753304
复制相似问题