我有两个包含时间信息的单元格数组:
这是一个例子:
'2012-05-10'
'2012-05-10'
'2012-05-10'
'2012-05-10'
'2012-05-10'
'2012-05-11'
'2012-05-11'
'19:28:27.000'
'19:28:38.000'
'21:57:31.000'
'21:57:37.000'
'21:57:40.000'
'21:57:43.000'
'21:57:50.000'
我只需要两个时间信息,比如我做的就是:
'2012-05-10' '19:28:27.000'
'2012-05-11' '21:57:40.000'
我也有一段时间也有这个:
'2012-05-10'
'2012-05-10'
'2012-05-10'
'2012-05-10'
'2012-05-10'
'19:26:27.000'
'19:26:38.000'
'21:55:31.000'
'21:57:37.000'
'21:55:40.000'
我如何做到这一点。
答案 0 :(得分:3)
您可以使用unique
功能执行此操作:
>> dates = {'01-Jan-2001'; '01-Jan-2001'; '01-Jan-2001'; '02-Jan-2001'};
>> times = {'15:52';'16:03';'17:05';'04:13'};
>> [d idx] = unique(dates);
>> t = times(idx);
>> [d t]
ans =
'01-Jan-2001' '17:05'
'02-Jan-2001' '04:13'
此方法获取与每个日期关联的 last 时间。如果您想抓住第一个时间,那么您可以使用此功能:
function [d t] = uniqueDates(dates,times)
[d idx] = unique(flipud(dates));
reversed_times = flipud(times);
t = reversed_times(idx);
答案 1 :(得分:1)
使用datevec()将字符串转换为vecotrs,然后删除min和sec信息并使用datestr()转换回单个字符串,然后使用unique()
这是一个例子,但我没有测试过:
dates = ['2012-05-10'
'2012-05-10'
'2012-05-10'
'2012-05-10'
'2012-05-10'
'2012-05-11'
'2012-05-11']
times = ['19:28:27.000'
'19:28:38.000'
'21:57:31.000'
'21:57:37.000'
'21:57:40.000'
'21:57:43.000'
'21:57:50.000']
%this bit might not work, if not just do it with a for loop. It is constructing a vecotr of spaces.
spaces = zeros(size(times,1), 1);
spaces(:) = " ";
%Concatenate the date and time strings with a space character between them.
DateVectors = datevec([dates, spaces, times]);
%discard min and sec
DateVectors(:, 5:6) = 0;
%convert back to strings
DateStrings = datestr(DateVectors);
%find unique values
unique(DateStrings)