precision while using anonymous function
2 次查看(过去 30 天)
显示 更早的评论
I have an anonymous function and the same operation using matrix manipulations. I am getting different results. what could be the issue
clc
X = rand(2000, 2000);
Npts = size(X,1);
alpha = 1;
d = 3;
kfunc = @(alpha, d, xi, xj) (alpha*(xi* xj') ).^d;
for j=1:Npts
K1(:,j) = kfunc( alpha, d, X, X(j,:));
end
K2 = ( alpha*(X*X')).^ d;
diff = abs(K2-K1);
sum(sum(diff))
0 个评论
回答(1 个)
Walter Roberson
2017-10-29
The difficulty is not that you used anonymous functions. The difficulty is that you did the calculation in a different way.
There are patterns of operations that MATLAB can recognize, and when it finds them, it passes the data on to a high performance multi-threaded library to do the calculations. That library will divide up the work and use all available cores, and then combine the results. The exact order that it splits up the calculations can depend upon the sizes and orientation involved.
Floating point calculations round off and the round-off depends upon the order of the calculations. http://matlab.wikia.com/wiki/FAQ#Why_is_0.3_-_0.2_-_0.1_.28or_similar.29_not_equal_to_zero.3F, so the fragments sliced different ways will have different round-offs, and so the recombinations will come out differently.
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Calculus 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!