Reputation: 99518
I would like to compute a weight as reciprocal of a distance for something like inverse distance weighting interpolation.
double wgt = 0, wgt_tmp, result = 0;
for (int i = 0; i < num; i++) {
wgt_tmp = 1.0/dist[i];
wgt += wgt_tmp;
result += wgt_tmp * values[i];
}
results /= wgt;
However the distance can be 0
and I need to make the weight suitable for computation. If there is only one distance dist[i]
is 0
, I would like its corresponding value values[i]
to be dominant. If there are several distances are 0
, I would like to have their values to contribute equally to the result. Also even if dist[i]
is not zero but very small, I would like to have a reasonable criterion to check it and deal with it. Any idea how to implement it?
Upvotes: 4
Views: 4850
Reputation: 617
If there is a zero-distance, there is no need of interpolation since you have a perfect match!
inside the for-loop:
if(dist[i] == 0.) return values[i];
Upvotes: 7
Reputation: 497372
I don't see any way besides piecewise - you need a different function than reciprocal distance for small distances. The simplest thing would be to just chop off the top:
modified_dist[i] = dist[i] < MIN_DIST ? MIN_DIST : dist[i]
but you could replace that with something still decreasing if you want, like (MIN_DIST + dist[i])/2
.
Upvotes: 6
Reputation: 22634
Come up with definitions of "dominant", "very small" and "deal with it". Then, translate them into code.
Upvotes: 5