Thread: Table of X & Y Values.

  1. #1
    Registered User
    Join Date
    Aug 2008
    Location
    Delaware
    Posts
    47

    Table of X & Y Values.

    I have several tables of X and Y values to store, and lookup based on the indices x and y.

    I need to store the tables in such a way thet I can retrieve the x, y intersection and interpolate between values of x and y.

    Code:
    table1 [8] [10] = {{0, 0, 0, 0, 0, 0, 0, 0, 0, 0},	
         								{0.92, 1.14, 0.80, 0.46, 0.34, 0.80, 0.80, 0.23, 0.46, 0.92},
         								{1.14, 1.37, 1.03, 0.92, 0.92, 1.14, 1.14, 0.80, 0.92, 1.14},
         								{1.49, 1.49, 1.37, 1.26, 1.14, 1.26, 1.26, 1.03, 1.26, 1.49},
         								{2.06, 1.94, 1.94, 1.72, 1.72, 1.72, 1.72, 1.14, 1.72, 2.06},
         								{2.52, 2.52, 2.17, 1.72, 1.72, 1.72, 1.72, 1.37, 1.72, 2.52},
         								{2.63, 3.32, 2.40, 1.72, 1.72, 1.72, 1.72, 1.49, 1.72, 2.63},
         								{3.09, 3.77, 2.75, 1.72, 1.72, 1.72, 1.72, 1.26, 1.72, 3.09}};
    x and y are floating point values and are not linear.

    I want to make the lookup algorithm a fuction, so I can use it on multiple tables.

  2. #2
    Registered User
    Join Date
    May 2008
    Posts
    8
    You need a function that look for an specific number (floating point) and returns the x and y values where it is found?

  3. #3
    Registered User
    Join Date
    Aug 2008
    Location
    Delaware
    Posts
    47
    Quote Originally Posted by ninboy View Post
    You need a function that look for an specific number (floating point) and returns the x and y values where it is found?

    No, I need a function that will take two floating point numbers (x, and y) and return the closest approximation from an 8 x 10 array.

    I could do it with a separate function for x, and another for y, and a third for the x. y intersection.

    Code:
     
    int xindex [8] = {-55.5, -30, -20, 0, 10, 22.5, 41.0, 52];
    int yindex [10] =[ 3000, 2100, 1500, 900, 600, 400, 300, 250, 220, 210};
    
    if (x <= -55.5) xindex = 1;
    else if ((x >-55.5) && (x<=  -30)) xindex =2;
         else.............);
    if (y >=3000) yindex = 1;
    else.........;
    
    functxy = table1 [xindex] [yindex];
    return functxy;

    I also have to interpolate between the values of x, and y in functxy, but didn't try to include it here.

  4. #4
    Registered User
    Join Date
    Aug 2008
    Location
    Delaware
    Posts
    47
    There's a couple of errors in the above code, but it's only included to demonstrate my problem.

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    So the if-elseif-elseif-etc is a perfectly good way of figuring out where in the table you need to be. But I would probably use auxiliary tables, a la
    Code:
    float xindex[] = {-55.5, -30, -20, 0, 10, 22.5, 41.0, 52 };
    .
    .
    .
    int i = 0;
    while ((i < 8) && (xindex[i] < target_x)) i++;
    which would find the table value one too high (and of course subtracting one for the one too low). You can do something similar for y.

    After that, you'll have to decide which of the 16345 ways to interpolate you intend to do.

  6. #6
    Registered User
    Join Date
    Aug 2008
    Location
    Delaware
    Posts
    47
    Quote Originally Posted by tabstop View Post
    So the if-elseif-elseif-etc is a perfectly good way of figuring out where in the table you need to be. But I would probably use auxiliary tables, a la
    Code:
    float xindex[] = {-55.5, -30, -20, 0, 10, 22.5, 41.0, 52 };
    .
    .
    .
    int i = 0;
    while ((i < 8) && (xindex[i] < target_x)) i++;
    which would find the table value one too high (and of course subtracting one for the one too low). You can do something similar for y.

    After that, you'll have to decide which of the 16345 ways to interpolate you intend to do.
    That looks like a good way to do what I want. I will also use an integer variable 'sizex' that can be passed in a function call for different size arrays. The same function can also be used to get the y index.

    Code:
    xindex[] = {-55.5, -30, -20, 0, 10, 22.5, 41.0, 52 };
    .
    .
    .
    int sizex = 8;
    int i = 0;
    while ((i < sizex) && (xindex[i] < target_x)) i++;
    return i;
    x = i;
    '
    '
    '
    y = i;
    functxy = table1 [x] [y];
    (interpolate here);
    return functxy;

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Dictionary in C# to hash table in C?
    By dinoman in forum C Programming
    Replies: 2
    Last Post: 04-12-2009, 09:23 PM
  2. ASP.NET question (looping inside a table)
    By Rainbowinblack in forum C# Programming
    Replies: 0
    Last Post: 03-06-2008, 03:10 PM
  3. Lookup table with different spacing between entries
    By Boksha in forum C++ Programming
    Replies: 2
    Last Post: 02-19-2008, 02:40 PM
  4. Replies: 1
    Last Post: 12-30-2007, 10:08 AM
  5. inputting words from a file
    By kashifk in forum C++ Programming
    Replies: 5
    Last Post: 10-24-2003, 07:18 AM