These two sections of codes for getting the mean should give the same answer, but why don't they?
1 view (last 30 days)
Show older comments
Apologies for the trivial and very possibly boring question, I'm very new to MATLAB; this comes from a MATLAB Fundamentals section task.
Here, the 315x4 matrix usage represents electricity use across four sectors, with each column representing one sector. The task asks the student to find the mean of each column, ignoring all NaN elements.
The given solution of the task is very straight-forward:
emean = mean(usage,"omitnan")
The answer is a row vector of four elements which represent the means of the four columns of usage, as expected.
Having forgot the existence of "omitnan", I used a much lengthier and relatively inefficient section of code:
RES = usage(:,1)
COM = usage(:,2)
IND = usage(:,3)
TOT = usage(:,4)
A = mean(RES(~ismissing(RES)))
B = mean(COM(~ismissing(COM)))
C = mean(IND(~ismissing(IND)))
D = mean(TOT(~ismissing(TOT)))
emean = [A B C D]
As far as I can tell, these two sections of codes should give the same output for emean, but for some reason, they don't. The first two elements differ by a very small amount.
What am I missing?
(For reference, I encountered this problem in Task 1 of the seventh page of Section 12.3 of MATLAB Fundamentals. MATLAB Fundamentals (mathworks.com))
5 Comments
Gatech AE
on 16 Jun 2021
Edited: Gatech AE
on 16 Jun 2021
@the cyclist, I looked at the data and the difference is 1E-8 relative a magnitude of 3E6, so it's the 15th digit of a double. Seems like error in 16 bit representation to me.
Stephen23
on 16 Jun 2021
Edited: Stephen23
on 16 Jun 2021
"should give the same answer..."
No, they should not: in general, there is no reason why two different algorithms operating on binary floating point numbers should provide exactly the same output values.
"...but why don't they?"
Different operations lead to different accumulated error. This is expected.
Learn about binary floating point numbers and how they behave:
This is worth reading as well:
Accepted Answer
Matt J
on 15 Jun 2021
The first two elements differ by a very small amount.
Likely just floating point noise.
0 Comments
More Answers (0)
See Also
Categories
Find more on Logical in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!