How do I create a column for occupied and unoccupied in which each value in the column is a linspace timeline? I understand the timeline length for each value will be different.

1 回表示 (過去 30 日間)
I need to keep historical data for when a room is occupied and unoccipied in order to predict occupancy in the near future. In order to do so I am trying to create a timeline (by the minute) using date-time format. Am I on the right track here? Should I be using "datestr()"? Thanks in advance.
occupied = zeros(N,1); %vector for times when the room is occupied
unoccupied = zeros(N,1); %vector for times when the room is unoccupied
for i=1:N %looping the build vectors
occupied(i)=linspace(starttime(i),endtime(i));
j=i+1;
unoccupied(i)=linspace(endtime(i),starttime(j));
end
  4 件のコメント
Adam Danz
Adam Danz 2019 年 12 月 20 日
"Am I on the right track here? "
Not the best track. A room is either occupied or unoccupied. That's binary. There's no reason to have two different variables. You only need a logical 1/0 (1 for occupied, 0 for unoccupied; or vise versa).
If you should show us a sample of your data it may be easier to suggest alternatives.
GS25
GS25 2019 年 12 月 20 日
Here is a sample of 30 of the start and end times for "occupied", I need the time to be unoccupied between the end time and the next start time:
'06/02/17 08:27' '06/02/17 14:55'
'06/05/17 08:54' '06/05/17 14:38'
'06/06/17 08:16' '06/06/17 13:08'
'06/07/17 06:24' '06/07/17 19:29'
'06/08/17 08:23' '06/08/17 13:47'
'06/08/17 13:58' '06/08/17 15:39'
'06/09/17 08:21' '06/09/17 20:42'
'06/11/17 09:43' '06/11/17 17:08'
'06/12/17 09:09' '06/12/17 13:05'
'06/12/17 14:12' '06/12/17 18:15'
'06/13/17 08:43' '06/13/17 18:18'
'06/14/17 08:33' '06/14/17 11:26'
'06/14/17 13:17' '06/14/17 16:04'
'06/15/17 07:36' '06/15/17 09:50'
'06/15/17 14:25' '06/15/17 18:44'
'06/16/17 07:40' '06/16/17 08:33'
'06/16/17 09:05' '06/16/17 10:57'
'06/16/17 11:02' '06/16/17 17:05'
'06/19/17 07:58' '06/19/17 11:41'
'06/19/17 15:14' '06/19/17 18:36'
'06/20/17 07:21' '06/20/17 16:03'
'06/21/17 09:53' '06/21/17 11:18'
'06/21/17 11:33' '06/21/17 12:57'
'06/21/17 13:56' '06/21/17 13:58'
'06/22/17 08:32' '06/22/17 13:02'
'06/22/17 14:47' '06/22/17 15:47'
'06/23/17 07:24' '06/23/17 07:50'
'06/23/17 08:38' '06/23/17 12:46'
'06/23/17 15:13' '06/23/17 17:07'
'06/24/17 13:35' '06/24/17 14:15'

サインインしてコメントする。

採用された回答

Steven Lord
Steven Lord 2019 年 12 月 20 日
Consider storing your data in a timetable. The first minute a room becomes occupied, assign it the value 1. The first minute it becomes unoccupied, assign it the value 0. Then retime the timetable to use the 'minutely' time basis and the 'prev' method.
  3 件のコメント
Adam Danz
Adam Danz 2019 年 12 月 20 日
編集済み: Adam Danz 2019 年 12 月 20 日
Here an implementation of Steven Lord's suggestion. See inline comments for details and 2 assumptions that must be met. Also see a note at the end.
startStopTimes = {
'06/02/17 08:27' '06/02/17 14:55'
'06/05/17 08:54' '06/05/17 14:38'
'06/06/17 08:16' '06/06/17 13:08'
'06/07/17 06:24' '06/07/17 19:29'
'06/08/17 08:23' '06/08/17 13:47'
'06/08/17 13:58' '06/08/17 15:39'
'06/09/17 08:21' '06/09/17 20:42'
'06/11/17 09:43' '06/11/17 17:08'
'06/12/17 09:09' '06/12/17 13:05'
'06/12/17 14:12' '06/12/17 18:15'
'06/13/17 08:43' '06/13/17 18:18'
'06/14/17 08:33' '06/14/17 11:26'
'06/14/17 13:17' '06/14/17 16:04'
'06/15/17 07:36' '06/15/17 09:50'
'06/15/17 14:25' '06/15/17 18:44'
'06/16/17 07:40' '06/16/17 08:33'
'06/16/17 09:05' '06/16/17 10:57'
'06/16/17 11:02' '06/16/17 17:05'
'06/19/17 07:58' '06/19/17 11:41'
'06/19/17 15:14' '06/19/17 18:36'
'06/20/17 07:21' '06/20/17 16:03'
'06/21/17 09:53' '06/21/17 11:18'
'06/21/17 11:33' '06/21/17 12:57'
'06/21/17 13:56' '06/21/17 13:58'
'06/22/17 08:32' '06/22/17 13:02'
'06/22/17 14:47' '06/22/17 15:47'
'06/23/17 07:24' '06/23/17 07:50'
'06/23/17 08:38' '06/23/17 12:46'
'06/23/17 15:13' '06/23/17 17:07'
'06/24/17 13:35' '06/24/17 14:15'};
% convert to a single vector of datetime
% format output as needed using 'Format'
% https://www.mathworks.com/help/matlab/ref/datetime.html#namevaluepairarguments
startStopTimes = sort(datetime(startStopTimes(:),'InputFormat','MM/dd/yy HH:mm'));
% Initialize table with alternating rows of start;stop times.
% This assumes that the start times (col 1) and stop times (col 2) do not contain
% any overlaps and are in chronological order. If those 2 assumptions are not
% true this will not work.
tf = repmat([true;false],numel(startStopTimes)/2,1);
TT = timetable(startStopTimes,tf,'VariableNames',{'Occupied'});
% resample at 1 minute resolution
TTr = retime(TT,'minutely','previous');
Result: the Occupied collumn shows when the room is occupied (true).
head(TTr) % show first few rows
ans =
8×1 timetable
startStopTimes Occupied
____________________ ________
02-Jun-2017 08:27:00 true
02-Jun-2017 08:28:00 true
02-Jun-2017 08:29:00 true
02-Jun-2017 08:30:00 true
02-Jun-2017 08:31:00 true
02-Jun-2017 08:32:00 true
02-Jun-2017 08:33:00 true
02-Jun-2017 08:34:00 true
Note: this interprets the StartTime as the first minute the room is occupied. It interprets the StopTime as the first minute the room is unoccupied. If the stop time is the last minute a room is occupied, you'll need to make this change:
% replace this
startStopTimes = sort(datetime(startStopTimes(:),'InputFormat','MM/dd/yy HH:mm'));
% with this
startStopTimes = datetime(startStopTimes,'InputFormat','MM/dd/yy HH:mm');
startStopTimes(:,2) = startStopTimes(:,2) + minutes(1);
startStopTimes = sort(startStopTimes(:));
GS25
GS25 2019 年 12 月 20 日
Thank you Adam and Steven for your help!

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeData Type Conversion についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by