# How can I find a general formula describing this piecewise function?

I have a set of scores $s \in[1;40]$, where $s$ is integer. I want to map each score to a index, like this:

$$1 \le s \le 5 \to 0 \\ 6 \le s \le 10 \to 1 \\ 11 \le s \le 20 \to2\\ 21 \le s \le 30 \to3\\ 31 \le s \le 40 \to4\\$$

So basically I am looking for a function which would map the score to the index. I have tried with logs and algebraic functions (is that the term?) but since the intervals are not the same (5 for the first, 10 for the second, 10 for the third, &c.) I don't know how to get around this.

-
Is $s$ an integer? – Jacob Sep 28 '12 at 16:08
You have precisely defined a function with your LaTeX code – Paul Slevin Sep 28 '12 at 16:10
@Jacob: Aw, yes. I always try to be complete but I always miss some details... – rubik Sep 28 '12 at 16:11
@PaulSlevin: Ok but I am looking for a rapresentation which would be suitable for a calculation. I will get those values randomly I have have to compute the index. – rubik Sep 28 '12 at 16:11
If you use this function in a program, it should be written as a for loop with an if statement inside... – xavierm02 Sep 28 '12 at 16:14

## 2 Answers

Using Heaviside step function $H$, this is $$\bigg\lceil\frac{s}{10}\bigg\rceil-H(5-s).$$

-
Thanks for the quick reply, but with $s = 10$ I get $2$ instead of $1$. – rubik Sep 28 '12 at 16:20
@rubik: For $s=10$ you have $H(5 - 10) = H(-5) = 0$... – Thomas Sep 28 '12 at 16:21
@Thomas: Oh yes sorry, my fault. – rubik Sep 28 '12 at 16:36
The two answers are both great, but I'm choosing this one just because I think it's simpler! – rubik Sep 29 '12 at 12:25

$$f(s)=\left\lfloor\frac {s-1}{10}\right\rfloor +\left\lfloor\frac {s+34}{40}\right\rfloor$$

-
Could you explain how the method works for constants $c_1,\dots,c_{n}$ and intervals $C_1,C_2,\dots,C_n$? – Pedro Tamaroff Sep 28 '12 at 16:25
Is this function always increasing? I would guess yes... – rubik Sep 28 '12 at 16:37
Any function $f:\{1,\ldots,n\}\to\mathbb R$ can be written as $\sum_{k=0}^{n-1}a_k\left\lfloor\frac{x+k}n\right\rfloor$ by taking $a_k=f(n-k)-f(n-k-1)$. Of course summands with $a_k=0$ can be dropped, and sometimes you can simplify. – Hagen von Eitzen Sep 28 '12 at 16:51