0

我有两个带有时间信息的单元格数组:

这是示例:

'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'

我该怎么做。

4

2 回答 2

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'

此方法获取与每个日期关联的最后时间。如果你想抢一次那么你可以使用这个功能:

function [d t] = uniqueDates(dates,times)
[d idx] = unique(flipud(dates));
reversed_times = flipud(times);
t = reversed_times(idx);
于 2012-07-16T14:42:18.690 回答
1

使用datevec () 将字符串转换为向量,然后删除 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)
于 2012-07-16T14:35:00.220 回答