2012-07-16 87 views
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' 

我如何去做这件事。

+1

你在这里问?你的问题有点不清楚 – mathematician1975 2012-07-16 13:16:04

+1

你应该提供更多的上下文和解释。我假设每个数组中的相同索引条目都是相同日期时间的部分。你的头衔说你想要一个平均值,但是你在一个特定的日子里取最小/最早的时间(假设在你的例子中你错误地被一个 - 最快的时间11日为下一个条目) – Mikeb 2012-07-16 13:17:32

+0

添加到前一个评论,预期的结果是什么?请提供一个简短的输入示例和预期输出... – 2012-07-16 13:37:20

回答

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

转换字符串使用datevec(),然后丢弃该分和秒信息和使用datestr转换回一个字符串(),然后使用唯一的()

下面是一个例子vecotrs但还没牛逼测试它:

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)