General Algorithm Question

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • billelev
    New Member
    • Nov 2006
    • 119

    General Algorithm Question

    Say there is a series of numbers 1, 2, 3, 4, 5, 6, 7, 8

    I want to map this series to another set of values. For example

    Series | Mapped Value
    1, 1
    2, 1
    3, 1
    4, 1
    5, 2
    6, 2
    7, 2
    8, 2
    9, 3
    10, 3
    etc.

    Does anyone know a good/general way of doing this in VBA, and therefore probably any other language? I've looked at using the Mod function, but it never seems as simple as it should be.
  • Curtis Rutland
    Recognized Expert Specialist
    • Apr 2008
    • 3264

    #2
    I'm not seeing the relationship you are posing....

    Comment

    • billelev
      New Member
      • Nov 2006
      • 119

      #3
      1 maps to 1
      2 maps to 1
      3 maps to 1
      4 maps to 1
      5 maps to 2
      ..
      8 maps to 2
      9 maps to 3
      ...
      12 maps to 3

      If the series goes from 1 to i, the following formula works, if the result is rounded to zero decimal places. Doesn't seem very elegant...

      Mapped Value = roundToZeroDeci mals[ (i - 2.5) / 4 ] + 1

      so for i = 7, mapped value = (7 - 2.5) / 4 + 1 = (4.5 / 4) + 1 = 2 (0 dp)

      Comment

      • gits
        Recognized Expert Moderator Expert
        • May 2007
        • 5390

        #4
        for javascript you could just use the following code to test in Firebug:

        [CODE=javascript]for ( var i = 1; i <= 20; i++) {
        console.info( i + ' -> ' + Math.ceil(i/4) );
        }[/CODE]
        so as you see you just need to divide the value by 4 and then ceil (round up to the next integer) the result ...

        kind regards

        Comment

        • billelev
          New Member
          • Nov 2006
          • 119

          #5
          Originally posted by gits
          for javascript you could just use the following code to test in Firebug:

          [CODE=javascript]for ( var i = 1; i <= 20; i++) {
          console.info( i + ' -> ' + Math.ceil(i/4) );
          }[/CODE]
          so as you see you just need to divide the value by 4 and then ceil (round up to the next integer) the result ...

          kind regards
          Great, thank you very much.

          Comment

          • gits
            Recognized Expert Moderator Expert
            • May 2007
            • 5390

            #6
            no problem - you're welcome ;)

            kind regards

            Comment

            Working...