Matlab provides different results for same formula
Show older comments
Hi guys,
I am asking this question in a rather dirty way now:

How is it possible that the "for loop results" on the index {425,79} differ from the results for "AAAAA"?
This is really really frustrating as I can't find a solution.
Currcallprices is a cell array which are either empty or filled with a 1001x1 double. Same holds true for Interpol_ki.
Many thanks for your help. I will provide further details immediately in case they are necessary.
Thanks in advance
Thomas
Accepted Answer
More Answers (1)
Walter Roberson
on 18 Aug 2017
interpol_ki{429,79} might be empty so that loop iteration might be skipped.
More generally, in MATLAB when it detects loops or arrays that are "sufficiently large" and which happen to match one of several code patterns, then MATLAB might hand of parts of the calculation to high performance libraries, and those libraries might not give a bitwise identical result.
For example, if you had
d = rand(1,100000);
t = 0;
for K = 1 : length(d)
t = t + d(K);
end
then this pattern of totaling might be detected, and instead of just adding the values one by one in a "plain reading" loop, it might be dispatched to a multiprocessor library that might end up doing the equivalent of
t1 = sum(d(1:25000)); %on one processor
t2 = sum(d(25001:50000)); %on processor #2
t3 = sum(d(50001:75000)); %on processor #3
t4 = sum(d(75001:100000)); %on processor #4
t = t1 + t2 + t3 + t4;
Algebraically these are the same as the original, but in floating point arithmetic, these two approaches can have different round-off error, just the same way that .1-.3+.2 is different than .1+.2-.3
1 Comment
Thorben Green
on 19 Aug 2017
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!