0

I have a set of numbers ranging from -1 to +1, I would like to rescale this set to the range 0 to 255, and also to rescale 0 to 1024, how can I create a generic function for this?

  • Welcome to Mathematics Stack Exchange. $512(x+1)$ should rescale (from $-1$ to $1$) to $0$ to $1024$ – J. W. Tanner Jan 17 '23 at 14:12
  • The general approach for mapping a range $[a,b]$ to a range $[A,B]$ is to find the equation of a line satisfying $f(a)=A$ and $f(b)=B$. Finding the equation of a line given two pieces of information like this should have been taught in middle school for most people. If you don't remember what you were taught in middle school then there exist plenty of youtube or online teaching tools like kahn academy to remind you. – JMoravitz Jan 17 '23 at 14:15
  • Note that if the $0$ ... $255$ range will be restricted to integers, you may want to map to the range $-0.5$ ... $255.4999$ and then round to the nearest integer. Otherwise the values $0$ and $255$ of the target range get a bit shortchanged. – Jaap Scherphuis Jan 17 '23 at 14:43

0 Answers0