我有一个数据矩阵,其中每行有几个日期时间和一个员工ID(相同的ID将在不同的行中重复)。我需要按员工评估日期时间,因此我希望将相关数据汇总在一起以便进行处理。我是Matlab的新手,并不太了解什么是最好的(结构数组,单元数组,单个数组等)。注意:我事先知道我将评估多少员工,但他们会有不同的日期时间,因此矩阵可能是错误的。
现在我循环遍历矩阵的行并检查用户列,我希望能够将该行的日期时间保存到该用户的位置(如果已存在某些用户,则追加)
data_matrix = [1,735724.291666667,735724.322916667,735724.343750000,5;
2,735724.331250000,735724.340277778,735724.371527778,18;
3,735724.402083333,735724.420833333,735724.445833333,18;
4,735724.411111111,735724.465277778,735724.515972222,42;
5,735724.412500000,735724.433333333,735724.459027778,8;
6,735724.425000000,735724.519444444,735724.552777778,5;
7,735724.431250000,735724.455555556,735724.482638889,18;
8,735724.438888889,735724.486805556,735724.495833333,8;
9,735724.531944444,735724.545138889,735724.581250000,18;
10,735724.547222222,735724.579861111,735724.604166667,13;
11,735724.549305556,735724.588194444,735724.614583333,8;
12,735724.563888889,735724.618055556,735724.645138889,42];
% we only care about IDs 1-26
employees_to_check= 1:26
for i= 1:length(data_matrix)
%employee id is the fifth element
this_employee = [data_matrix(i,5)];
keep_record = ismember(this_row_tech, employees_to_check);
if keep_record
times_to_add = [data_matrix(i,2), data_matrix(i,3), data_matrix(i,4)]
% save it someplace
end
end
保存这样的数据的好方法是什么?
答案 0 :(得分:1)
以下是您案例中如何使用表格的简短示例:
dataTable = array2table(data_matrix,'VariableNames',...
{'serial','time1','time2','time3','employeeID'});
dataTable.time1 = datetime(datestr(dataTable.time1));
dataTable.time2 = datetime(datestr(dataTable.time2));
dataTable.time3 = datetime(datestr(dataTable.time3));
% we only care about IDs 1-26
valid = dataTable.employeeID>=1 & dataTable.employeeID<=26;
employees_to_check = dataTable(valid,:);
times_to_add = employees_to_check{:,2:4};
所以你的表dataTable
看起来像是:
dataTable =
serial time1 time2 time3 employeeID
______ ____________________ ____________________ ____________________ __________
1 05-May-2014 07:00:00 05-May-2014 07:45:00 05-May-2014 08:15:00 5
2 05-May-2014 07:57:00 05-May-2014 08:10:00 05-May-2014 08:55:00 18
3 05-May-2014 09:39:00 05-May-2014 10:06:00 05-May-2014 10:42:00 18
4 05-May-2014 09:52:00 05-May-2014 11:10:00 05-May-2014 12:23:00 42
5 05-May-2014 09:54:00 05-May-2014 10:24:00 05-May-2014 11:01:00 8
6 05-May-2014 10:12:00 05-May-2014 12:28:00 05-May-2014 13:16:00 5
7 05-May-2014 10:21:00 05-May-2014 10:56:00 05-May-2014 11:35:00 18
8 05-May-2014 10:32:00 05-May-2014 11:41:00 05-May-2014 11:54:00 8
9 05-May-2014 12:46:00 05-May-2014 13:05:00 05-May-2014 13:57:00 18
10 05-May-2014 13:08:00 05-May-2014 13:55:00 05-May-2014 14:30:00 13
11 05-May-2014 13:11:00 05-May-2014 14:07:00 05-May-2014 14:45:00 8
12 05-May-2014 13:32:00 05-May-2014 14:50:00 05-May-2014 15:29:00 42
,输出为:
times_to_add =
05-May-2014 07:00:00 05-May-2014 07:45:00 05-May-2014 08:15:00
05-May-2014 07:57:00 05-May-2014 08:10:00 05-May-2014 08:55:00
05-May-2014 09:39:00 05-May-2014 10:06:00 05-May-2014 10:42:00
05-May-2014 09:54:00 05-May-2014 10:24:00 05-May-2014 11:01:00
05-May-2014 10:12:00 05-May-2014 12:28:00 05-May-2014 13:16:00
05-May-2014 10:21:00 05-May-2014 10:56:00 05-May-2014 11:35:00
05-May-2014 10:32:00 05-May-2014 11:41:00 05-May-2014 11:54:00
05-May-2014 12:46:00 05-May-2014 13:05:00 05-May-2014 13:57:00
05-May-2014 13:08:00 05-May-2014 13:55:00 05-May-2014 14:30:00
05-May-2014 13:11:00 05-May-2014 14:07:00 05-May-2014 14:45:00