2
$\begingroup$

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.

  • 0
    Is $s$ an integer?2012-09-28
  • 0
    You have precisely defined a function with your LaTeX code2012-09-28
  • 1
    @Jacob: Aw, yes. I always try to be complete but I always miss some details...2012-09-28
  • 0
    @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.2012-09-28
  • 0
    If you use this function in a program, it should be written as a for loop with an if statement inside...2012-09-28
  • 0
    I want to avoid ifs and for loops if possible. That's why I asked for a mathematical solution to the problem.2012-09-28
  • 0
    @rubik: There is nothing non-mathematical about using piece-wise defined functions. But if you might not want to use piece-wise defined functions.2012-09-28
  • 0
    Note that in a program, a lookup from a table of 40 entries is typically faster than computing two divisions (as in my solution below).2012-09-28
  • 0
    @Thomas There, it was piece-wise that I was missing. Yes I prefer a non piece-wise defined function.2012-09-28
  • 0
    @HagenvonEitzen: Thanks for the tip, but this time I'm not particularly concerned with speed. So I would say I can ignore the difference in performance between a lookup table and two division operations.2012-09-28

2 Answers 2

2

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

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

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

  • 2
    Could you explain how the method works for constants $c_1,\dots,c_{n}$ and intervals $C_1,C_2,\dots,C_n$?2012-09-28
  • 0
    Is this function always increasing? I would guess yes...2012-09-28
  • 1
    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.2012-09-28