Thread: Nearest value in array

  1. #1
    Registered User
    Join Date
    Apr 2007

    Nearest value in array


    Say I have a 2D array with some data
    that holds a resistance and what temperature that result gives.

    Now I do a a calculation that gives me a resistance. But in my array i have not got that exact resistance.

    So I need to find the nearest value.

    Whats possible ways to find the nearest value in my array?

  2. #2
    Registered User
    Join Date
    Aug 2005
    The nearest value from the table is that one where
    abs( val_calc - val_table ) is smallest.

  3. #3
    Registered User
    Join Date
    Sep 2006
    A for loop through the rows, with another for loop to scan through the columns, nested inside the outer row loop (along with the basic math to test each value), is all you'll need.

    You can set a variable "BestValue" to the first index's nearness to the proper value in the table, and then test each value as it's calculated to see if it's closer than the "BestValue" is, so far. If it is, then it becomes the new "BestValue". You'll want to save the BestValue's index number maybe, as well.


  4. #4
    Registered User
    Join Date
    Apr 2007
    Thanks Guys.

    No idea why i didn't think of this before.

    Silly me.

    But thanks again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 05-29-2009, 07:25 PM
  2. from 2D array to 1D array
    By cfdprogrammer in forum C Programming
    Replies: 17
    Last Post: 03-24-2009, 10:33 AM
  3. [question]Analyzing data in a two-dimensional array
    By burbose in forum C Programming
    Replies: 2
    Last Post: 06-13-2005, 07:31 AM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  5. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM