I am having this issue where this spread of numbers is not returning the correct value in matlab.
=MEDIAN (A1:A34) is giving me -.380905 which is correct.
A=[-0.264, 0.4459, -0.49781, 1.77666,-0.55638,0.87174,-0.68504,0.92835,-0.80581,-0.87505,0.39111,-0.76054,-0.68987,1.60776,-0.19637,1.13956,1.53606,-0.08254,0.12186,0.08428,0.61663,-1.47958,2.28422,-0.80891,-0.55738,0.2238,-0.93291,0.3791,-0.63074,2.14683,-1.49948,1.21954,-0.79734,-0.51303,-1.0687,-0.61345,-1.02592,-0.87653,0.444]
M = median(A)ReturnsM = -0.2640.
I did this calculation in MATLAB which is wrong.
Edit: I figured out the calculation was actually right in excel but wrong in Matlab. It seems that Matlab only picks one of two middle numbers in an even array.
13 Answers
Returns M = -0.2640 I did this calculation in matlab.
You've even number of values, so the median might be interpreted differently
- Excel just takes the mean of the two middle items (that's what I've seen so far in other tools too)
- matlab gives you one of the middle values, but again as there are even values, no exact middle exists
To expand on @Máté Juhász's answer, here is one of the remarks when using the Median function:
If there is an even number of numbers in the set, then
MEDIANcalculates the average of the two numbers in the middle. See the second formula in the example.
You have 34 numbers. After ordering them, the 2 numbers in the middle are -0.49781 and -0.264. Their average is:
(-0.49781-0.264)/2=-0.380905So Excel is giving the correct answer as stated on the remarks.
As @Máté Juhász said, you got only one of the middle numbers as an answer when using Matlab.
Your example has 39 values, so, after sorting the middle one is -0.2640. It was too bad that Matlab made such a gross mistake.
1