SitePoint Sponsor

User Tag List

Results 1 to 10 of 10
  1. #1
    SitePoint Evangelist
    Join Date
    Jan 2005
    Location
    UK
    Posts
    539
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    find closest value in array

    I'm trying to draw a graph for a 24 hr period.
    My data array is $arr[$time]=$value
    I need to find in the array the times closest to every 15mins and get the time and value for each...

    ie

    00:15
    00:30
    00:45

    etc...

    Currently they are more like 00:04, 00:14, 00:24 etc...but not necessarily these!

    Then i need to build a new array with the new times and the values of their closest time.

    What's the best way to do this?

    Also, as this is plotting a full day and at the time the graph is created it may only be part of the way through a day, i then need to fill the new array with blank values for the remainer of the day's times.
    The only other issue is that sometimes i don't get values for a number of hours and so i would need to insert blank values in the new array for those times.

  2. #2
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    Questions:

    Currently they are more like 00:04, 00:14, 00:24 etc...but not necessarily these!
    a) In those 3 examples should those go the nearest, or to the next time slot down?

    ie should the above map to;

    1) 00:00, 00:15, 00:30
    OR
    2) 00:15, 00:15, 00:30

    b) is the 00:04 part of a timestamp coming from say, a database? or is it a single field coming from a user-input form?

  3. #3
    SitePoint Evangelist
    Join Date
    Jan 2005
    Location
    UK
    Posts
    539
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'm only going to pick the clostes ones and discard the rest so...

    00:04 => 0.1541
    00:14 => 0.1542 get this and make it 00:15 => 0.1542
    00:24 => 0.1543
    00:34 => 0.1544 get this and make it 00:30 => 0.1544
    00:44 => 0.1545
    etc

    The time is indeed coming from db column set to time. I had been using explode to get rid of the secs, so from 00:04:15 to 00:04

  4. #4
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    Sorry to appear pedantic, more questions:

    c) what happens when you have this

    00:04 => 0.1541
    00:14 => 0.1542
    00:16 <-- does this go up or down?**
    00:24 => 0.1543

    ** If it goes up, how do you resolve the fact that it is 1 minute off 00;15 just as 00:14 is?

    d) Is there a finite amount of values, or is it always a fight-off between 2?

    Will this likely happen?

    00:04
    00:05
    00:06
    00:14
    00:16
    00:17

    e)
    00:14 => 0.1542 get this and make it 00:15 => 0.1542
    Is the emboldened value relevant?

  5. #5
    SitePoint Evangelist
    Join Date
    Jan 2005
    Location
    UK
    Posts
    539
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    c) In that case we would have to pick one and ignore the other. It doesn't matter which if they are like this.

    I'm ignoring fact that its 1 min off, and just changing value to equal that new time!

    d) values end at 23:59. That won't likely happen.

    e) thats the value that i need for the new time. Of course this is an example value in this case. I have made the value for the closest time = the value for the new regulated time

    Hope i understood you correctly!

  6. #6
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    Have you thought of turning this problem on its head, and just getting the closest value from your database for each 15 minute period?

    //pseudo sql statement
    where yourtime is closest to 2009-11-04 13:14:00 LIMIT 1

    The guys on the mysql forum are amazing.

    re; d) No you misunderstood my question:

    d) Is there a finite amount of values, or is it always a fight-off between 2?

    Will this likely happen?

    14:04 // 3 values vying for 14:00
    14:05
    14:06
    14:14 // and 4 values vying for 14:15
    14:16
    14:17
    14:18

    Or is it only ever 2 values slugging it out.

    (ps I have no idea how to solve your problem yet, btw, still trying to nail what your incoming data is and your parameters for success - if anyone else has a solution please weigh in, I'm off for lunch)

  7. #7
    SitePoint Evangelist
    Join Date
    Jan 2005
    Location
    UK
    Posts
    539
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    yes maybe mysql statement is answer, should i post over there you think?

  8. #8
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    If you can provide them with a small sample of what the data may look like in your database, a column or 2 with say, half a dozen rows containing the timestamps:

    2009-11-05 14:04:01
    2009-11-05 14:05:45
    2009-11-05 14:06:43
    2009-11-05 14:14:19
    2009-11-05 14:16:01
    2009-11-05 14:17:57
    2009-11-05 14:18:03

    and say you are looking for a query which will select the single closest match to

    2009-11-05 14:15:00

    ie number #4 on that list

    then you might be pleasantly surprised (or not).

    How you iterate through each 15 minute period MIGHT also be achievable in sql, but one thing at a time eh?

    They are very knowledgeable over there, and saved me a lot of PHP jiggery-pokery.

  9. #9
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Some graphing libraries can kinda fill in missing points for you by estimating a value based on the known points before and after it.

    It's been a long time, but I think it was called cubic splines.

    So, you could probably just grab all the points you do know, and let it do the work after telling it how many points you want drawn and at what interval.

  10. #10
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    That would be supposing the OP wants all the point plotted, whereas I understand he only wants one out of each 15 minute period.

    Perhaps this is more a case of
    // pseudo
    if ( anytimes found between 14:08 and 14:23 )
    add to plot "14:15"


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •