4

For a program I write I need some optimal values. I calculated them using Mathematica and I get something like this:

{2375, 2556, 2758, 2974, 3201, 3677, 4173, 5068}

Since my program only accepts specific values like ...

{2000, 2800, 3600, 4200, 5500}

... I will need to round my calculated values to those. That means that every calculated value should be replaced by the closest accepted number.

This is only an example. I calculated more than a hundred values and dont want to round them by myself. How can I use Mathematica to achieve this?

Arji
  • 1,134
  • 7
  • 10

1 Answers1

8

Here is one way

data = {2375, 2556, 2758, 2974, 3201, 3677, 4173, 5068};
vals = {2000, 2800, 3600, 4200, 5500};

Nearest[vals, #] & /@ data // Flatten (* {2000, 2800, 2800, 2800, 3600, 3600, 4200, 5500} *)

Rohit Namjoshi
  • 10,212
  • 6
  • 16
  • 67