Suppose that I have an ordered list of numbers. The numbers in the list may be evenly spaced, but they may not be. Here are two examples, list1
and list2
:
list1 = {0, 0.2, 0.4, 0.6, 0.8, 1};
list2 = {0, 0.3, 0.4, 0.7, 0.9, 1};
I would like to write a function to determine the midpoints (or "midvalues"). So, I would like to write fun
such that with the following input I will obtain the following output:
fun[list1]
fun[list2]
{0.1, 0.3, 0.5, 0.7, 0.9} {0.15, 0.35, 0.55, 0.8, 0.95}
Is there any built-in way to do this in Mathematica 8? I am looking for a simple method. I came up with the following, which seems to work fine, but I am afraid I may be overcomplicating it:
midpoint[a_, b_] := (a + b)/2
fun[list_List] := Map[Apply[midpoint, #] &, Partition[list, 2, 1]]
where
fun[list1]
fun[list2]
gives
{0.1, 0.3, 0.5, 0.7, 0.9} {0.15, 0.35, 0.55, 0.8, 0.95}
Do you have any suggestions to make this simpler?