필터 지우기
필터 지우기

Extrapolate the times of entry and exit in timetable

조회 수: 4 (최근 30일)
Erica Corradi
Erica Corradi 2018년 7월 3일
댓글: Peter Perkins 2018년 7월 3일
I have a timetable: I want to extrapolate the times of entry and exit from a certain room. I did a for loop on rows and two if loops: the first, I take the column for all the rooms on the i-th row (pir_tab_night.Column1streamId (i)) and I place it == at the room I'm interested in; I also added the condition that the start time must be 0 (in such a way as to take the first line in which the person enters the room) ... here it gives me error (Comparison is not defined between datetime and double arrays). the second if cycle is for the time out of the room: within this cycle I calculate the vect_duration for each (t_end-t_start) and then I go to concatenate with the function cat(1, vect_duration, t_end-t_start). Finally I repositioned the times = 0 to start a new count. This is my code:
vect_duration = []; t_start = 0; t_end = 0; for i = 1:length(pir_tab_night.t) if pir_tab_night.Column1streamId(i) == 'dbuid-17' & t_start == 0 t_start = pir_tab_night.t(i); end if pir_tab_night.Column1streamId(i) ~= 'dbuid-17' & t_start ~= 0 t_end = pir_tab_night.t(i-1); vect_duration = t_end - t_start; vect_duration = cat(1, vect_duration, t_end-t_start); t_start = 0; t_end = 0; end end
Can someone help me? Thank you

채택된 답변

Paolo
Paolo 2018년 7월 3일
편집: Paolo 2018년 7월 3일
%Find dbuid-17 in your second column.
dbuid17indx = pir_tab_night.Column1streamId==categorical({'dbuid-17'});
%Use diff function to determine difference between entries.
diffindx = diff(dbuid17indx);
%Convert diffindx to char. Find patterns: 1, variable numbers of 0s, forward slash /.
[startIndx,endIndx] = regexp(char(diffindx+'0')','(10*/)');
%Group all entry and exit times.
times = arrayfun(@(x,y) pir_tab_night.t(x+1:y),startIndx,endIndx,'un',0);
times is a cell array, where
times{1} =
12-Sep-2017 21:38:45
12-Sep-2017 21:39:33
times{2} =
12-Sep-2017 23:23:43
12-Sep-2017 23:24:19
times{3} =
13-Sep-2017 04:37:12
13-Sep-2017 04:39:04
times{4} =
13-Sep-2017 06:26:34
13-Sep-2017 06:26:55
13-Sep-2017 06:27:32
... and so on. times contains the dates corresponding to every "entry" and "exit", as indicated by dbuid-17 in your second column.
  댓글 수: 6
Erica Corradi
Erica Corradi 2018년 7월 3일
Thank you so much for solving my problem !! Can I ask you to explain me why you put the +'0' and '(10*/)' in the regexp function?
Peter Perkins
Peter Perkins 2018년 7월 3일
This is a great vectorized solution to problem that seems like it would need a loop but really doesn't. A couple of minor readability suggestions:
1) it probably isn't necessary to explicitly construct a categorical value in creating dbuid17indx, I think this
dbuid17indx = pir_tab_night.Column1streamId=='dbuid-17';
should work.
2) Might be more readable to use seconds(3) rather than duration(0,0,3).
3) regexp is pretty challenging. If I'm reading the code correctly, it might be easier to just find positive and negative elements of diffindx.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by