# Value for x such that |(x-y)| is minimum

I have a list

Y={y1,y2,y3,...,yn}


And I would need to find x such that

Abs[x-Y[[i]]] for all $y_i$ in $Y$ is minimum.

I unfortunatly can't remember my highschool Statistics courses where this function was defined.

-
Do you perhaps mean to minimize the sum over the |x-y_i|? – Andrew Jaffe Aug 6 '13 at 7:57
Are you sure you want to ask in Mathematica (the software) forum? Isn't this rather about Maths (math.stackexchange.com)? – Pinguin Dirk Aug 6 '13 at 8:01
That was a fast accept. By the way, the value that minimizes the sum of absolute deviations is called the median, and using Mathematica's built-in function for it will probably be a better idea. – Rahul Aug 6 '13 at 8:56
@RahulNarain How do you and Nasser know it is about sum? :) – Kuba Aug 6 '13 at 9:26
@Kuba I feel more confident in my guess now that the asker has accepted Nasser's answer! – Rahul Aug 6 '13 at 9:30

ClearAll[x]
npts = 10;
y = RandomReal[{0, 1}, npts];
z = x /. FindMinimum[Total[Abs[#] & /@ (x - y)], x][[2]];
ListPlot[{{{1, z}, {npts, z}}, y}, Joined -> True, Mesh -> All]


Appendix Comments above said to use Median for this. But this test shows result of Median and what I have above is not the same. Tried it on different random lists:

ClearAll[x]
npts = 10;
minAbs[y_] := Module[{x}, x /. FindMinimum[Total[Abs[#] & /@ (x - y)], x][[2]]];
z = Table[y = RandomReal[{0, 1}, npts]; {minAbs[y], Median[y]}, {10}];
Map[Abs@Differences[#] &, z]


gives

{{0.0210295415415782}, {0.0034994693789796}, {0.000485836594898592}, \
{0.00186973168510612}, {0.000737313633257242}, {0.00571989492676428}, \
{0.00631402466773001}, {0.0449264216282105}, {0.0881414864517821}, \
{0.0000334557733309149}}


So, there is a difference? Did I do something wrong?

-
When there is an even number of values, any value between the middle two is a minimizer; in other words, the median is not unique. Try comparing the value of Total[Abs[... on both your result and the median. Then try the whole thing again with npts = 11. – Rahul Aug 6 '13 at 10:27