# how to find interval of data

36 views (last 30 days)
elisa ewin on 3 Jun 2016
Answered: Elias Gule on 3 Jun 2016
Hi! I have a dataset of trajectories of users: every current location of the traiectories has these fields:_ [userId year month day hour minute second latitude longitude regionId]. Based on the field day, I want to divide trajectories based on daily-scale in interval of different hours: 3 hours, 4 hours, 2 hours. I have realized this code that run for interval of 4 hours
% decomposedTraj is a struct that contains the trajectories based on daily scale
for i=1:size(decomposedTraj,2)
if ~isempty(decomposedTraj(i).dailyScaled)
% find the intervals
% interval [0-4]hours
Interval(i).interval_1=(decomposedTraj(i).dailyScaled(:,5)>=0&decomposedTraj(i).dailyScaled(:,5)<4);
% interval [4-8]hours
Interval(i).interval_2=(decomposedTraj(i).dailyScaled(:,5)>=4&decomposedTraj(i).dailyScaled(:,5)<8);
% interval [8-12]hours
Interval(i).interval_3=(decomposedTraj(i).dailyScaled(:,5)>=8&decomposedTraj(i).dailyScaled(:,5)<12);
% interval [12-16]hours
Interval(i).interval_4=(decomposedTraj(i).dailyScaled(:,5)>=12&decomposedTraj(i).dailyScaled(:,5)<16);
% interval [16-20]hours
Interval(i).interval_5=(decomposedTraj(i).dailyScaled(:,5)>=16&decomposedTraj(i).dailyScaled(:,5)<20);
% interval [20-0]hours
Interval(i).interval_6=(decomposedTraj(i).dailyScaled(:,5)>=20);
end
end
or more easily to understand the logic of the code:
A=[22;19;15;15;0;20;22;19;15;15;0;20;20;0;22;21;17;23;22]';
A(A>=0&A<4)
A(A>=4&A<8)
A(A>=8&A<12)
A(A>=12&A<16)
A(A>=16&A<20)
A(A>=20)
It runs and gives the right answer but it's not smart: if I want to change the interval, I have to change all the code... can you help me to find a smart solution more dinamical of this? thanks

Elias Gule on 3 Jun 2016
Will this do:
interval = 2; % 2 hour-interval maxHr = 23; tmstamp = 0 : interval : maxHr; len = length(tmstamp);
Within your loop, add the following loop: A = decomposedTraj(i).dailyScaled(:,5); for index = 2 : len predicate = A >= tmstamp(index-1) & A < tmstamp(index); Interval(i).(sprintf('interval_%d',index-1)) = A(predicate); if index == len Interval(i).(sprintf('interval_%d',index)) = A(A >= tmstamp(end)); end end
Now your code should look like: A. Before the for loop, enter the following lines:
interval = 2; % 2 hour-interval
maxHr = 23;
tmstamp = 0 : interval : maxHr;
len = length(tmstamp);
B. now your loop should be:
% decomposedTraj is a struct that contains the trajectories based on daily scale
for i=1:size(decomposedTraj,2)
if ~isempty(decomposedTraj(i).dailyScaled)
% Get data
A = decomposedTraj(i).dailyScaled(:,5);
% Update the Interval struct array
for index = 2 : len
predicate = A >= tmstamp(index-1) & A < tmstamp(index);
Interval(i).(sprintf('interval_%d',index-1)) = A(predicate);
if index == len
Interval(i).(sprintf('interval_%d',index)) = A(A >= tmstamp(end));
end
end
end
end