[英]matlab bsxfun numerical precision
我有兩個Euclidian距離函數,一個使用bsxfun
,另一個使用repmat
。 他們在OSX的Matlab 2012a上得出的結果略有不同。 例如
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)));
你會得到相同的結果
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.