TransWikia.com

Median function in matlab not returning the correct value

Super User Asked by Atemp on December 16, 2021

I am having this issue where this spread of numbers is not returning the correct value in matlab.

https://docs.google.com/spreadsheets/d/1jOPRVZR_XJ-v1nEEvuMfzoJOh702A4wg/edit#gid=1868814066

=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)

Returns
M = -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.

2 Answers

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 MEDIAN calculates 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.380905

So 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.

Answered by Reddy Lutonadio on December 16, 2021

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

Answered by Máté Juhász on December 16, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP