These two sections of codes for getting the mean should give the same answer, but why don't they?

1 view (last 30 days)
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
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
Stephen23 on 16 Jun 2021
Edited: Stephen23 on 16 Jun 2021

Sign in to comment.

Accepted Answer

Matt J
Matt J on 15 Jun 2021
The first two elements differ by a very small amount.
Likely just floating point noise.

More Answers (0)

Products

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!