如何转换十进制小时并每5分钟计算一次平均值?

时间:2013-11-16 03:18:10

标签: matlab accumarray

我是Matlab的初学者,我需要解决一个问题。首先,我需要将UT columm从十进制小时转换为小时:分钟:秒。然后,我需要平均每5分钟计算一次ROT柱并以新矩阵显示回复(小时:分:秒,腐烂均值)。

数据

UT(第1列)矩阵
5.4
5.404
5.408

ROT(column2)矩阵

0.22

0.123

0.129

e.g。 UT(5.404)= 0.404 * 60 = 24.252; 0.252 * 60 = 15.12,然后UT(5.404)= 5:24:15小时:分钟:秒

提前致谢

马塞洛

2 个答案:

答案 0 :(得分:1)

首先将十进制小时日期转换为统一为一天的连续日期:

serdates = [5.4;5.404;5.408]/24;

然后使用datestr转换为字符串(这是一个整形操作):

datestr(serdates,'HH:MM:SS')

在5分钟箱中观察组(1b <= x <上升):

ymdhms      = datevec(serdates);
[~,minbins] = histc(ymdhms(:,5),[0:5:60])

按年,日,月,小时和5分钟分组:

[untime,~,subs] = unique([ymdhms(:,1:4) minbins*5],'rows')

累积腐烂:

rot5min = accumarray(subs,[0.22;0.123;0.129]);

对于更高级的演示文稿收集到带有约束的数据集

dataset({ cellstr(datestr(datenum([untime,zeros(size(untime,1),1)]),31)),'Datetime'}, {rot5min 'ROT5min'})

ans = 
    Datetime                     ROT5min
    '0000-01-00 05:05:00'        0.472  

答案 1 :(得分:0)

这样做。欢呼声。

function v=myfunction(ut,rot)
% ut is a vector of decimal hours
% rot is a vector of the same length as ut
% v is a matrix with rows of the form (hour, min, sec, rot5) where
%   rot5 is an average of rot over 5 min interval from 2.5 min in the past
%   to 2.5 min to the future.

m=numel(ut);

% input validation
assert(isvector(ut) && isvector(rot) && numel(rot)==m);

% array initialization
v=zeros(m,4);
utCopy=ut;

for i=1:m
    % calculate hour from decimal hour
    v(i,1)=floor(utCopy(i));

    % calculate minute from decimal hour
    utCopy(i)=(utCopy(i)-v(i,1))*60;
    v(i,2)=floor(utCopy(i));

    % calculate second from decimal hour, round to nearest integer
    utCopy(i)=(utCopy(i)-v(i,2))*60;
    v(i,3)=round(utCopy(i));

    % calculate 5 mins central average of rot
    % to get forward average just replace the logical indexing with
    % ut>=ut(i) & ut<=ut(i)+1/12
    v(i,4)=mean(rot(ut>=ut(i)-1/24 & ut<=ut(i)+1/24));
end

end
相关问题