Re: eliminate values while caculating Mean[data]
- To: mathgroup at smc.vnet.net
- Subject: [mg92000] Re: eliminate values while caculating Mean[data]
- From: Mariano Suárez-Alvarez <mariano.suarezalvarez at gmail.com>
- Date: Tue, 16 Sep 2008 19:25:19 -0400 (EDT)
- References: <gag2rl$3fq$1@smc.vnet.net> <gal3e6$drc$1@smc.vnet.net>
On Sep 15, 4:38 am, kazik.lak... at gmail.com wrote:
> Hello Pasha,
> I would generally recommend you to write your own functions, it's more
> customizable:)
> I wrote the function you need. Here it is:
>
> The name of your set of data is "Data" and looks like in your post,
> say: Data={1,2,3,4,5}; Value of "Summ" gives you a sum of all non-zero
> elements in "Data" and "NonZeroValuesCounter" gives you a number of
> non-zero entries in "Data". "Average" is what you want. Just paste the
> script below into Mathematica and run:
>
> Summ = 0; NonZeroValuesCounter = 0;
> For[index = 1, index <= Length[Data], index += 1,
> If[Data[[index]] != 0, Summ += Data[[index]];
> NonZeroValuesCounter += 1];
> ];
> Average = Summ/NonZeroValuesCounter;
> Print["Summ: ", Summ, "
> NonZeroValuesCounter: ", NonZeroValuesCounter, "
> Average: ", Average]
Even if you insist in doing things that way, you
can do them more Mathematica-ly: For example,
myMean[ll_] := Module[{n = 0, s = 0},
Scan[If[# > 0, s += #; n += 1] &, ll];
s/n
];
-- m