I have to write an expression for the following. I have values, one fpr each of the last 5 months:
Month: 1 - 989
Month: 2 - 0
Month: 3 - 234
Month: 4 - 0
Month: 5 - 098
I need to the calculate average value from these last 5 months which should be calculated only for non zero values. How can I write a common formula for that?
Thanks.
Since an average is a quotient of a sum and the number of samples (which we can itself write as sum), one way to encode this to incorporate the condition into the sum. For example, if we denote the month by $a$ and the value for the month $a$ by $v_a$, we can write $$\text{(average)} = \frac{\sum_{v_a \neq 0} v_a}{\sum_{v_a \neq 0} 1}.$$ The denominator here is just the number of months with nonzero values $v_a$.
Of course, the condition $v_a \neq 0$ is unnecessary for the sum in the numerator, as removing it simply includes more zero terms, which don't contribute to the sum. Also, this notation leaves implicit the index set of months $a$.