- Convert Initial Timestamp: We use datetime to handle the initial timestamp, which allows for easy manipulation and extraction of time components.
- Iterate Over Intervals: For each time interval in t, we calculate the cumulative time since the initial timestamp t0 using minutes(sum(t(1:i))).
- Check Hour Intervals: We extract the hour from the current timestamp and check whether it falls within the specified intervals [a, b] or [c, d].
- Store Indices: Based on the check, we store the index in either d1 or d2.
Clustering GPS times based on the hour of the day
2 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Gabe Hopper
el 30 de Nov. de 2017
Comentada: Gabe Hopper
el 31 de Ag. de 2024
Hello,
I have some raw GPS data that is sampled every 10 minutes or so. I also have the initial timestamp that is the initial recording date and time. I am trying to write a function that returns the index of observations that falls into particular hours of the day, ie. in the hour intervals [ a, b] and [ c, d] (24h clock) and also outside these hours. I try to divide observations contained into these two intervals vs. those outside of these intervals. Thus, all values that fall inside the one of the two intervals are in a vector ' d1' and values outside are in vector ' d2'.
More precisely, my data looks like this:
timeStamp = '16/01/2010__00:01:00' %t0
t %a 1*n vector of time intervals: t = [...,9,10,11,10,10,9,8,10,...]
h %vector of time intervals to split the serie: h = [a,b,c,d]
I would like my function to return the indexes of values that fall to one of the two sets ( d1 or d2). The vector t can be large (more than 30000 observations) and I would ideally like something
For instance: if h = [1,4,18,22] and current t(i) translates to 7h21m56s (inside), it will be set in partition ' d2', if t(i) translates to 22h12m57s (thus outside the interval) in partition ' d1' and so on.
I made an utterly complicated function long time ago that doesn't seem to work. I am thus looking for something simpler and accurate.
My pseudo solution was:
function [d1, d2] = Create_TimeClasses(t, timeStamp, h)
%Create time classes (millisecond precision):
t0 = datenum(timeStamp);
%Reconstruct date vector:
k=1; l=1;
formatOut = 'HH:MM:SS.FFF';
ted = t0;
for i=2:size(t,2)
MM = floor(t(i));
ss = t(i)-MM;
SS = floor(ss*100); %2-digits seconds;
FFF = round((ss*100-floor(ss*100))*1000); %3-digits milliseconds precision.
%recreate the string: format: HH:MM:SS.FFF (FFF milliseconds) to take
%into account the side effects:
S = strcat('0:',num2str(MM), ':', num2str(SS), '.', num2str(FFF));
Str = datestr(S,formatOut); %String should now be ok
%Then gather the time and extract the number of milliseconds:
[~,~,~,hours,minutes,seconds] = datevec(Str, formatOut);
mSt = round(1000*(3600*hours + 60*minutes + seconds)); %milliseconds
%add to date
ted = addtodate(ted, mSt, 'millisecond');
aaa = datestr(ted, formatOut);
hed = str2double(aaa(1:2)); %hours
med = str2double(aaa(4:5)); %minutes
sed = str2double(aaa(7:8)); %seconds
%Correct for additional seconds:
if(sed/0.6 >= 1)
med = med+1;
end
hed = hed + mod(med/60, 24) + sed;
%Interesting activity time zone?
if(hed >= h(1) && hed <= h(2) || hed >= h(3) && hed <= h(4))
d1(k) = i; %yes
k=k+1;
else %non-activity time zone:
d2(l) = i;
l=l+1;
end
end
Thank you
0 comentarios
Respuesta aceptada
Abhinaya Kennedy
el 19 de Ag. de 2024
To simplify your function, you can convert the timestamp to MATLAB's datetime format (https://www.mathworks.com/help/matlab/ref/datetime.html), which simplifies time calculations. You can then iterate over your time intervals and determine if each timestamp falls within the specified hour intervals.
function [d1, d2] = Create_TimeClasses(t, timeStamp, h)
% Convert the initial timestamp to a datetime object
t0 = datetime(timeStamp, 'InputFormat', 'dd/MM/yyyy__HH:mm:ss');
% Initialize vectors for storing indices
d1 = [];
d2 = [];
% Iterate over each time interval
for i = 1:length(t)
% Calculate the current timestamp
currentTimestamp = t0 + minutes(sum(t(1:i)));
% Extract the hour from the current timestamp
currentHour = hour(currentTimestamp);
% Check if the current hour falls within the specified intervals
if (currentHour >= h(1) && currentHour <= h(2)) || (currentHour >= h(3) && currentHour <= h(4))
d1 = [d1, i]; % Add index to d1
else
d2 = [d2, i]; % Add index to d2
end
end
end
This function should be more efficient and easier to understand than the original approach. Make sure to test it with your data to ensure it meets your needs.
Más respuestas (0)
Ver también
Categorías
Más información sobre Dates and Time en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!