Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
matlab bsxfun数值精度_Matlab_Bsxfun - Fatal编程技术网

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谢谢!