matlab bsxfun数值精度
我有两个欧几里德距离函数,一个使用matlab bsxfun数值精度,matlab,bsxfun,Matlab,Bsxfun,我有两个欧几里德距离函数,一个使用bsxfun,另一个使用repmat。他们在Matlab2012a,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 =
bsxfun
,另一个使用repmat
。他们在Matlab2012a,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)
给出:
为什么会这样,或者我遗漏了什么?您执行的操作顺序不同。像这样插入括号
d1 = sqrt(abs(repmat(xx', [1 size(yy, 2)]) + (repmat(yy, [size(xx, 2) 1]) - 2*xy)));
您将得到相同的结果您执行的操作顺序不同。像这样插入括号
d1 = sqrt(abs(repmat(xx', [1 size(yy, 2)]) + (repmat(yy, [size(xx, 2) 1]) - 2*xy)));
您将得到相同的结果有关原因的更多信息,请参见链接的答案。@TroyHaskin谢谢!有关原因的更多信息,请参阅链接答案,并从中获得。@TroyHaskin谢谢!