Marcelo Duarte
Marcelo Duarte

Reputation: 23

How to convert decimal hour and calculate the average every 5 min?

I'm a beginner at Matlab and I need to solve a problem. First, I need to convert the UT columm from decimal hour to hour:minute:seconds. Then, I need to calculate every 5 minutes on average the ROT collumn and show the reply in a new matrix (hour:min:sec,rot mean).

Data

UT (column 1) A matrix
5.4
5.404
5.408

ROT (column2) A matrix

0.22

0.123

0.129

e.g. UT (5.404)=0.404*60=24.252; 0.252*60=15.12 ,then UT(5.404)=5:24:15 hours:min:sec

Thanks in advance

Marcelo

Upvotes: 2

Views: 2016

Answers (2)

Oleg
Oleg

Reputation: 10676

First convert decimal hour dates into serial dates where unity is a day:

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

Then convert to string with datestr (this is however a cosmetic operation):

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

Group observation in 5 minute bins (lb <= x < up):

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

Group then by year, day, month, hour and 5 minute bins:

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

Accumulate rot:

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

And for fancier presentation collect into dataset with datestrings

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  

Upvotes: 1

heriantolim
heriantolim

Reputation: 457

This will do. Cheers.

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

Upvotes: 0

Related Questions