1

I have generated a number z from a range (x,y) and want to map z to another range (p,q)

This question appears to be answered here using matlab:

https://www.mathworks.com/matlabcentral/answers/379380-mapping-of-a-random-number-in-one-range-to-another-range

I need to do this in java but also need to understand what this 'interp1' is doing.

bigcodeszzer
  • 916
  • 1
  • 8
  • 27

3 Answers3

1

Apache commons-math has several interpolators available:

https://commons.apache.org/proper/commons-math/javadocs/api-3.0/org/apache/commons/math3/analysis/interpolation/package-summary.html

There's also readily available documentation for matlab's interp1 fucntion, if you just want to know more about what it's capable of:

https://www.mathworks.com/help/matlab/ref/interp1.html

Izzy
  • 239
  • 1
  • 5
1

From my understanding, you are looking for this vq = interp1(x,v,xq) particular function in Matlab. x is the original range, v is the original value (one or multiple), xq is the new range.

Matlab states "vq = interp1(x,v,xq) returns interpolated values of a 1-D function at specific query points using linear interpolation."

Wiki about Linear Interpolation, it is basically what you are trying to implement, mapping number from one range to another range.

I found this will probably solve your problem, check it out. Mapping a numeric range onto another

hydroPenguin
  • 66
  • 1
  • 4
1

interp1, with default "linear" interpolation method, uses straight line that passes via points (x, p) and (y, q) to find Y-coordinate of the point with X-coordinate equal to z.

static double interp1(double x, double y, double p, double q, double z) {
  return p + (q - p) * (z - x) / (y - x);
}
Alex Filatov
  • 4,768
  • 2
  • 16
  • 10