elis56
elis56

Reputation: 121

matlab: how to find interval of data

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 0 Comments

Upvotes: 1

Views: 88

Answers (1)

ThP
ThP

Reputation: 2342

Interval k is defined as [(k-1)*N k*N] where N=4 in your example. Therefore you can do the same using a for loop:

for k=1:floor(24/N)
   Interval(k) = A(A>=(k-1)*N & A<k*N);
end

Note that in this example A(A>=(k-1)*N & A<k*N) is not necessarily the same size for each k so Interval should be a cell array.

Upvotes: 2

Related Questions