0
$\begingroup$

I have a data whose list plot shows up following graph

enter image description here

I would like to interpolate the missing data and make the function continuous but for particular x-coordinate there is multiple values of $y$ coordinate. If possible I would like to have on value of $y$ coordinate which is average of all $y$ coordinate if $x$ coordinate of these points are equal. I get the error

Interpolation::inddp: The point 0.4793349708578781` in dimension 1 is duplicated. >>

The sample of data is too big to post it here so it's here http://pastebin.com/iCp2j4YZ I can do this using other language but will I do it in MMA?

$\endgroup$
4
$\begingroup$

I couldn't figure out a quick way to import the data how you had your paste formatted (with curly brackets for each element, but no outer curly brackets) so I reformatted it and repasted it.

data = Import["http://pastebin.com/raw/V8807EsY", "Table"];

You say you'd like to average the duplicate points, so using Mean in combination with GatherBy should work,

interp = Interpolation[Mean /@ GatherBy[data, First]]

Interpolation::inddp: The point 0.9216985231092443` in dimension 1 is duplicated. >>

but it doesn't since you have so many digits in your numbers. By this, I mean that the difference between the two x-values in question (0.9216985231092445 and 0.9216985231092443) is equal to $MachineEpsilon. This is an easy workaround,

interp = Interpolation[
  Mean /@ GatherBy[data, Round[First[#], 10^-14] &]]


Show[
 Plot[interp[x], {x, 0.4, 14}, Axes -> False, Frame -> True],
 ListPlot[data, PlotStyle -> Red]
 ]

enter image description here

A longer, easier to read version of the code above is

interp = Interpolation[
  Map[Mean,
   GatherBy[data,
    Function[x, Round[First[x], 10^-14]]
    ]
   ]
  ]

The base command is GatherBy which applies the function given to each element of the list, and sorts those which give the same result into sublists. So any element of your list who would give the same result when you take its first element and round it to 0.00000000000001 is then grouped together in a sublist. The result of GatherBy is then a list of lists. Now you can just Map the function Mean onto each of these sublists (taking the average of their x and y values).

$\endgroup$
  • $\begingroup$ just a small stupid question, what does symbol & mean at the end of Round[]? $\endgroup$ – Santosh Linkha Feb 1 '16 at 9:48
  • $\begingroup$ It's for defining a pure function. Not a stupid question at all, that's part of the shorthand we tend to use around here that makes it easier to type, but perhaps harder to understand. I will type out a longer version of the command as well. $\endgroup$ – Jason B. Feb 1 '16 at 9:50
  • $\begingroup$ See the edit for an explanation, and see this answer for a great guide to the shorthand notation. $\endgroup$ – Jason B. Feb 1 '16 at 9:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.