Mathematica Stack Exchange is a question and answer site for users of Mathematica. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Is there an equivalent command of WeightedData (new in version 9) in Mathematica 8 or I have to define it by myself? I haven't found anything in the site or googleing it... If there is not such command do you have some suggestions for defining it?

share|improve this question
In order to create your own WeightedData you need to define it separately for every function that is supposed to accept it, I think. E.g. Unprotect[Mean]; Mean[WeightedData[data_,weight_]]:= data.weight/Total[w];. You should now be to do this: obj = WeightedData[data,weights]; Mean[obj]. – C. E. Dec 18 '13 at 12:30
up vote 5 down vote accepted

It might be useful to consider TagSet here, to provide a new rule to Mean when it encounters your new data object:

weightedData/:Mean[data_weightedData] :=Mean[List@@data[[1]]]

You can style your object so it looks the same as the M9 object:

Format[weightedData[data_]]:=Panel["weightedData[" <> ToString[Length[data]] <> "]"];

Now a function that builds your data object, there should be more error checking than currently implemented and MapThread was the first thing I thought of:

weightedData[data_, weights_] /; Length[data] == Length[weights]:= Module[{weighted},
weighted = MapThread[#1/#2 &,

Make some data and test it:


To make other functions play nice just use TagSet again

weightedData /: ListLinePlot[data_weightedData]:=ListLinePlot[List @@ data[[1]]];

enter image description here

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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