OIO
OIO

Reputation: 363

Efficient Array Normalization of Approximate Values

I'm looking for the efficient way of replacing approximate values (ushort[100]) for their target values.

There are two target values (ushort x, 2x), each value in the ushort[] approximates one of these two.

Upvotes: 0

Views: 466

Answers (3)

OIO
OIO

Reputation: 363

var a = target1 > words[i] ? target1 - words[i] : words[i] - target1;
var b = target2 > words[i] ? target2 - words[i] : words[i] - target2;
(OR)
var as = target1 - words[i];
var bs = target2 - words[i];
a = as + (as >> 31) ^ (as >> 31);
b = bs + (bs >> 31) ^ (bs >> 31);

if (a < b)
   normalized[i] = target1;
else
   normalized[i] = target2;

Upvotes: 0

Jim Brissom
Jim Brissom

Reputation: 32929

You could always just define a distance metric that allows you to assigns each of the approximate values to the expected values, considering those to be "bins", as in a histogram. Processing the values then means replacing the approximate value with the known value that has the smallest distance to that value.

Upvotes: 1

Henk Holterman
Henk Holterman

Reputation: 273264

  • make a sorted array with the expected values
  • for every approx, binary search for the closest expected
  • replace

Upvotes: 1

Related Questions