Thread Subject: Time Difference

Subject: Time Difference

From: Karan

Date: 1 Sep, 2007 15:52:17

Message: 1 of 9

I was wondering if anyone can help me with this

Have a huge vector of serial date time and i wish to
compute the difference between each element of the vector
with the next element. Basically the pseudo command is
something like this

find(diff(serial_Time_vector)>1800 seconds)

I wish to find the index numbers in the vector where the
difference is greater than 30 minutes. Whats the quickest
way to do this?

Thanks

Karan

Subject: Time Difference

From: Lorenzo

Date: 1 Sep, 2007 16:41:20

Message: 2 of 9

"Karan " <ksumbaly@hotmail.com> wrote in message
<fbc1rh$6n$1@fred.mathworks.com>...
> I was wondering if anyone can help me with this
>
> Have a huge vector of serial date time and i wish to
> compute the difference between each element of the vector
> with the next element. Basically the pseudo command is
> something like this
>
> find(diff(serial_Time_vector)>1800 seconds)
>
> I wish to find the index numbers in the vector where the
> difference is greater than 30 minutes. Whats the quickest
> way to do this?
>
> Thanks
>
> Karan

Karan,
I can't think of a better solution than your code... and it should be quite fast
even for a very large time vector.
I guess your problem has to do with the fact that you have a "serial date time"
vector rather than a sequential time vector in seconds. If that is the case,
search for "time series" in the online help. It looks like the function DATENUM
can help.

Subject: Time Difference

From: Karan

Date: 1 Sep, 2007 17:12:43

Message: 3 of 9

Lorenzo

The only issue is that my code doesnt work.
find(diff(datevec(serial_Time_vector))>1800) is also
something that wont work because datevec returns vector as
the date time.

Now I could send the datevec(serial_Time_vector) into a
separate function that could change the vector into a
format that i might like i.e seconds, minutes etc
i.e. time_in_seconds = convert_time(datevec
(serial_Time_vector),'seconds') to return the serial time
vector as seconds.

Hence the overall function shall look like:

find(diff(time_in_seconds)>1800)

The only issue now remains is that to get that serial time
changed to a vector (using datevec) and then to seconds (by
presumably using a for loop to go through each element of
the datevec returned value) is going to take agesss!!
Unless someone out there has a faster code to do something
like this?

Cheers

Karan


"Lorenzo " <lorenz4matlab@gmail.com> wrote in message
<fbc4ng$gjh$1@fred.mathworks.com>...
> "Karan " <ksumbaly@hotmail.com> wrote in message
> <fbc1rh$6n$1@fred.mathworks.com>...
> > I was wondering if anyone can help me with this
> >
> > Have a huge vector of serial date time and i wish to
> > compute the difference between each element of the
vector
> > with the next element. Basically the pseudo command is
> > something like this
> >
> > find(diff(serial_Time_vector)>1800 seconds)
> >
> > I wish to find the index numbers in the vector where
the
> > difference is greater than 30 minutes. Whats the
quickest
> > way to do this?
> >
> > Thanks
> >
> > Karan
>
> Karan,
> I can't think of a better solution than your code... and
it should be quite fast
> even for a very large time vector.
> I guess your problem has to do with the fact that you
have a "serial date time"
> vector rather than a sequential time vector in seconds.
If that is the case,
> search for "time series" in the online help. It looks
like the function DATENUM
> can help.

Subject: Time Difference

From: Karan

Date: 1 Sep, 2007 17:48:25

Message: 4 of 9

ok lorenzo the best i could do was

vec = round(datevec(sorted_data.Time));
dec_time = vec(:,6)+vec(:,5)*60+vec(:,4)*3600+vec(:,3)
*24*3600;
index_gt_30mins =find(diff(dec_time)>time_difference);

assuming that in the diff(dec_time) we dont get a vector
scenario of this sort
[
0 0 0 0 0 0 1
0 1 0 0 0 0 1
0 0 0 0 0 0 1
]

then the code above fails....

"Lorenzo " <lorenz4matlab@gmail.com> wrote in message
<fbc4ng$gjh$1@fred.mathworks.com>...
> "Karan " <ksumbaly@hotmail.com> wrote in message
> <fbc1rh$6n$1@fred.mathworks.com>...
> > I was wondering if anyone can help me with this
> >
> > Have a huge vector of serial date time and i wish to
> > compute the difference between each element of the
vector
> > with the next element. Basically the pseudo command is
> > something like this
> >
> > find(diff(serial_Time_vector)>1800 seconds)
> >
> > I wish to find the index numbers in the vector where
the
> > difference is greater than 30 minutes. Whats the
quickest
> > way to do this?
> >
> > Thanks
> >
> > Karan
>
> Karan,
> I can't think of a better solution than your code... and
it should be quite fast
> even for a very large time vector.
> I guess your problem has to do with the fact that you
have a "serial date time"
> vector rather than a sequential time vector in seconds.
If that is the case,
> search for "time series" in the online help. It looks
like the function DATENUM
> can help.

Subject: Time Difference

From: Lorenzo

Date: 1 Sep, 2007 18:03:35

Message: 5 of 9

Can you post (part of) your initial data?

Subject: Time Difference

From: Karan

Date: 2 Sep, 2007 05:40:40

Message: 6 of 9

hi lorenzo

Mailed u a copy as well

but here it goes

733283
733283
733283
733283
733283
733283.0001
733283.0001
733283.0001
733283.0001
733283.0001
733283.0001
733283.0001
733283.0001
733283.0002
733283.0002
733283.0002
733283.0002
733283.0002
733283.0002
733283.0002
733283.0002
733283.0002
733283.0003
733283.0003
733283.0003
733283.0003
733283.0003
733283.0003
733283.0003
733283.0003
733283.0003
733283.0004
733283.0004
733283.0004
733283.0004
733283.0004
733283.0004
733283.0004
733283.0004
733283.0005
733283.0005
733283.0005
733283.0005
733283.0005
733283.0005
733283.0005
733283.0005
733283.0005
733283.0006
733283.0006
733283.0006
733283.0006
733283.0006
733283.0006
733283.0006
733283.0006
733283.0006
733283.0007
733283.0007
733283.0007
733283.0007
733283.0007
733283.0007
733283.0007
733283.0007
733283.0008
733283.0008
733283.0008
733283.0008
733283.0008
733283.0008
733283.0008
733283.0008
733283.0008
733283.0009
733283.0009
733283.0009


"Lorenzo " <lorenz4matlab@gmail.com> wrote in message
<fbc9hn$67j$1@fred.mathworks.com>...
> Can you post (part of) your initial data?
>

Subject: Time Difference

From: Lorenzo

Date: 2 Sep, 2007 15:07:11

Message: 7 of 9

"Karan " <ksumbaly@hotmail.com> wrote in message
<fbdico$a9b$1@fred.mathworks.com>...
> hi lorenzo
>
> Mailed u a copy as well
>
> but here it goes
>
> 733283
> 733283
<snip>
> 733283.0009
>
>
> "Lorenzo " <lorenz4matlab@gmail.com> wrote in message
> <fbc9hn$67j$1@fred.mathworks.com>...
> > Can you post (part of) your initial data?
> >
>

Ok... from the Matlab documentation and these numbers, I gather that this is
an array of values in "Serial Date Number" format, which "represents the
whole and fractional number of days from a specific date and time, where
datenum('Jan-1-0000 00:00:00') returns the number 1". (from the help page
for datenum).
So, basically, these numbers are the *days* from "beginning of time".
Let's call Time the vector you posted. It can be transformed in seconds using
this code:

secsInADay = 24*3600; % seconds in a day
Time_days = Time - Time(1); % first element is the zero time
Time_sec = Time_days * secsInADay;

It turns out that the file you emailed me represents 24 hrs sampled at 1
second. Is this correct? If it is, then the code above is correct and your
original problem would be solved by

find( diff(Time_sec) > 1800 )

Lorenzo

Subject: Time Difference

From: Karan

Date: 11 Sep, 2007 10:45:52

Message: 8 of 9

Thanks Lorenzo

I actually took an easy way out. Its not foolproof but hey
it works

start_time = datevec(startTime);
        start_time = start_time(:,6)+start_time(:,5)
*60+start_time(:,4)...
            *3600+start_time(:,3)*24*3600;


"Lorenzo " <lorenz4matlab@gmail.com> wrote in message
<fbejiv$gef$1@fred.mathworks.com>...
> "Karan " <ksumbaly@hotmail.com> wrote in message
> <fbdico$a9b$1@fred.mathworks.com>...
> > hi lorenzo
> >
> > Mailed u a copy as well
> >
> > but here it goes
> >
> > 733283
> > 733283
> <snip>
> > 733283.0009
> >
> >
> > "Lorenzo " <lorenz4matlab@gmail.com> wrote in message
> > <fbc9hn$67j$1@fred.mathworks.com>...
> > > Can you post (part of) your initial data?
> > >
> >
>
> Ok... from the Matlab documentation and these numbers, I
gather that this is
> an array of values in "Serial Date Number" format,
which "represents the
> whole and fractional number of days from a specific date
and time, where
> datenum('Jan-1-0000 00:00:00') returns the number 1".
(from the help page
> for datenum).
> So, basically, these numbers are the *days*
from "beginning of time".
> Let's call Time the vector you posted. It can be
transformed in seconds using
> this code:
>
> secsInADay = 24*3600; % seconds in a day
> Time_days = Time - Time(1); % first element is the zero
time
> Time_sec = Time_days * secsInADay;
>
> It turns out that the file you emailed me represents 24
hrs sampled at 1
> second. Is this correct? If it is, then the code above is
correct and your
> original problem would be solved by
>
> find( diff(Time_sec) > 1800 )
>
> Lorenzo

Subject: Time Difference

From: us

Date: 11 Sep, 2007 11:37:30

Message: 9 of 9

Karan:
<SNIP where's the time warp...

one of the solutions

% create some sorted dates
     ds=[
          datestr(now:now+5)
          datestr(now+21:now+25)
     ];
% the engine
% ...use <datenum>
     dn=datenum(ds);
     ix=find(diff(dn)>10);
% the result, eg,
     ds(ix-1:ix,:)

us

Tags for this Thread

Everyone's Tags:

Add a New Tag:

Separated by commas
Ex.: root locus, bode

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Tag Activity for This Thread
Tag Applied By Date/Time
diff us 11 Sep, 2007 07:40:21
datenum us 11 Sep, 2007 07:40:21
code us 11 Sep, 2007 07:40:21
date Lorenzo 1 Sep, 2007 12:45:21
time series Lorenzo 1 Sep, 2007 12:45:21
time Lorenzo 1 Sep, 2007 12:45:20
rssFeed for this Thread

Contact us at files@mathworks.com