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:
I need to do this in java but also need to understand what this 'interp1' is doing.
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:
I need to do this in java but also need to understand what this 'interp1' is doing.
Apache commons-math has several interpolators available:
There's also readily available documentation for matlab's interp1 fucntion, if you just want to know more about what it's capable of:
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
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);
}