从MATLAB中的两个不同时间向量中提取特定时间点
Extracting specific time points from two different time vectors in MATLAB
我有两个时间向量:
V1 = ['02-Feb-2018 08:23:42' '02-Feb-2018 08:24:42'.... '02-Feb-2018 09:10:42']
V2 = [' 8:35 AM' ' 8:36 AM'...' 9:01 AM'].
现在,我想确定 V2
的起点 (t0
) 和终点 (tend
) 的索引(在本例中为 t0 = '8:35 AM'
和 tend = '9:01 AM'
) in V1
并初始化向量 V3
(长度 V1
),其中包含 t0
和 tend 之间的 '1'
,以及 '0'
在其他时间点。由于 v1
和 V2
的格式不同,我不知道如何在这里使用 datestr
。如果 V2
与 V1
:
的格式相同,则以下代码有效
V1=['02-Feb-2018 08:23:42'; '02-Feb-2018 08:24:42';'02-Feb-2018 09:10:42'];
V2 = [' 8:35 AM' ;' 8:36 AM';' 9:01 AM'];
V1=datenum(V1);
V2=datenum(V2);
[~,t0]=min(abs(V2(1)-V1));
[~,tend]=min(abs(V2(end)-V1));
我建议在这里使用 datetime
rather than datenum
。虽然 datenum
可以工作,但使用 datetime
为我们提供了一组更有用的方法,并允许我们更轻松地将 V2
的值标准化为 [=17] 的日期=].一种方法从 V1
获取日期并将它们分配给 V2
,这允许使用开始和结束时间进行直接逻辑比较。
例如:
V1 = ["02-Feb-2018 08:23:42"; "02-Feb-2018 08:24:42"; "02-Feb-2018 08:45:15"; "02-Feb-2018 09:10:42"];
V2 = ["8:35 AM"; "8:36 AM"; "9:01 AM"];
% Convert to datetime
d1 = datetime(V1);
d2 = datetime(V2, 'InputFormat', 'hh:mm a'); % Will assign today for date
% Assume all dates in V1 & V2 are the same
[y, m, d] = ymd(d1(1)); % Extract year, month, day from d1
[d2.Year, d2.Month, d2.Day] = deal(y, m, d); % Set date of d2 to that of d1
% Find min/max of d2
t_0 = min(d2);
t_end = max(d2);
% Generate V3
V3 = zeros(size(V1));
V3((d1 >= t_0 & d1 <= t_end)) = 1;
哪个returns:
>> V3.'
ans =
0 0 1 0
不出所料。
请注意,我已将时间戳添加到 V1
,该时间戳落在 V2
生成的时间 window 内,您的原始示例没有。我还使用了 strings (introduced in R2016b) to avoid having to whitespace pad a character array. If you're using an older version of MATLAB you should use a cell array 的数组。
我有两个时间向量:
V1 = ['02-Feb-2018 08:23:42' '02-Feb-2018 08:24:42'.... '02-Feb-2018 09:10:42']
V2 = [' 8:35 AM' ' 8:36 AM'...' 9:01 AM'].
现在,我想确定 V2
的起点 (t0
) 和终点 (tend
) 的索引(在本例中为 t0 = '8:35 AM'
和 tend = '9:01 AM'
) in V1
并初始化向量 V3
(长度 V1
),其中包含 t0
和 tend 之间的 '1'
,以及 '0'
在其他时间点。由于 v1
和 V2
的格式不同,我不知道如何在这里使用 datestr
。如果 V2
与 V1
:
V1=['02-Feb-2018 08:23:42'; '02-Feb-2018 08:24:42';'02-Feb-2018 09:10:42'];
V2 = [' 8:35 AM' ;' 8:36 AM';' 9:01 AM'];
V1=datenum(V1);
V2=datenum(V2);
[~,t0]=min(abs(V2(1)-V1));
[~,tend]=min(abs(V2(end)-V1));
我建议在这里使用 datetime
rather than datenum
。虽然 datenum
可以工作,但使用 datetime
为我们提供了一组更有用的方法,并允许我们更轻松地将 V2
的值标准化为 [=17] 的日期=].一种方法从 V1
获取日期并将它们分配给 V2
,这允许使用开始和结束时间进行直接逻辑比较。
例如:
V1 = ["02-Feb-2018 08:23:42"; "02-Feb-2018 08:24:42"; "02-Feb-2018 08:45:15"; "02-Feb-2018 09:10:42"];
V2 = ["8:35 AM"; "8:36 AM"; "9:01 AM"];
% Convert to datetime
d1 = datetime(V1);
d2 = datetime(V2, 'InputFormat', 'hh:mm a'); % Will assign today for date
% Assume all dates in V1 & V2 are the same
[y, m, d] = ymd(d1(1)); % Extract year, month, day from d1
[d2.Year, d2.Month, d2.Day] = deal(y, m, d); % Set date of d2 to that of d1
% Find min/max of d2
t_0 = min(d2);
t_end = max(d2);
% Generate V3
V3 = zeros(size(V1));
V3((d1 >= t_0 & d1 <= t_end)) = 1;
哪个returns:
>> V3.'
ans =
0 0 1 0
不出所料。
请注意,我已将时间戳添加到 V1
,该时间戳落在 V2
生成的时间 window 内,您的原始示例没有。我还使用了 strings (introduced in R2016b) to avoid having to whitespace pad a character array. If you're using an older version of MATLAB you should use a cell array 的数组。