Average similar time periods MATLAB - matlab

I have a matrix with a timestamp and several column variables.
The matrix spans a month of half hourly variables. Here is a sample of four columns of the matrix
11/11/2015 20:15 31.26410236 35.70104634 35.93171056
11/11/2015 20:45 32.10746291 35.48806277 35.9647747
.
.
.
12/11/2015 20:15 32.10746291 35.48806277 35.9647747
12/11/2015 20:45 32.10746291 35.48806277 35.9647747
.
.
.
13/11/2015 20:15 32.68310429 35.58753807 37.26447422
13/11/2015 20:45 33.05141516 34.8432801 36.48033884
.
.
.
14/11/2015 20:15 32.08328579 34.66482668 34.65446868
14/11/2015 20:45 32.19994433 34.40562145 34.34035989
What is the easiest way to find the average of identical times in terms of hours and minutes?
E.g. mean of each variable at time 20:45 for all days of the month.
I know I could achieve this by converting the timestamp to a datenum, taking the fractional part of datenum and sorting the data by the fractional part of datenum. After that I could block average the rows with similar fractional datenums. Is there a more efficient and more elegant way?

With matlab you can work directly with date and times without converting it to timestamp in miliseconds or seconds:
http://es.mathworks.com/help/matlab/date-and-time-operations-1.html
Or an easy way is to convert dates to a date vector like this:
DateVector = datevec(DateString,formatIn)
then compare the columns you want:
[Y,M,D,H,MN,S] = datevec(___)
>> A = datevec('13/11/2015 20:45','dd/mm/yyyy HH:MM')
A =
2015 11 13 20 45 0
>> B = datevec('14/11/2015 20:45','dd/mm/yyyy HH:MM')
B =
2015 11 14 20 45 0
with this is easy to compare dates:
>> A - B
ans =
0 0 -1 0 0 0
exactly one day difference

This is what I ened up doing to solve this problem:
timestamp=linspace(datenum('2015-11-01 00:00', 'yyyy-mm-dd HH:MM'),datenum('2015-12-01 00:00', 'yyyy-mm-dd HH:MM'),1440); % 30 days
timestamp=timestamp';
time_of_day=datetime(datevec(timestamp(1:48)),'Format','HH:mm');
numdays=30;
data=rand(length(timestamp),2);
means=NaN(48,3);
for tt=1:48
means(tt,:)=[datenum(time_of_day(tt)) nanmean(data(tt:48:48*numdays,:),1)];
end
figure;
plot(time_of_day,means(:,2:3));
xlim([timestamp(1) timestamp(48)]);

Related

Can't figure out date time conversion from int to datetime

I have an application that stores date+time as int in a database.
I would like to get back from the int to the real date&time!
I have these 2 examples:
919326588 ---> 25.03.2022 09:46
919322562 ---> 23.03.2022 14:43
I don't get it, even though I tried with julian days, unix-epoch, seconds from 1.1.1970, ....
Is there anybody out there who can help?
What I got so far is that the beginning is something like:
( 919362588 / 2000 ) + 2000000 --> 2459663.2939999998
The int part is the julian day 2022-03-24. The fraction part should be the fraction of a day in hours, minutes, ...
But it must be more than 0.5 to get over the 00:00 to 2022-03-25, but is only .2939999998 which is less than 0.5 .
assuming that the people dealing with these dates do not count from 12 - 12 but instead from 0 - 0 we have to add 0.5 to get from 12 - 0 midnight.
So this would mean:
0.2939999998 + 0+5 = 0.7939999998
But is 2022-03-25 07:03:21 not 09:46 :-(

Tableau - How to calculate date/time difference and result in full date/time?

I'm trying to calculate a difference between connection time and disconnected time. See image below. But DATEPART formula that I'm using only allows me to use one parameter (hour, minute, second,...)
However, as in the image, I have an ID where disconnection at 3/1/17 2:35:22PM and connection back at 3/2/17 1:59:38 PM
Ideal Response: 23 hours, 24 minutes and 16 seconds
but using the formula:
ZN(LOOKUP(ATTR(DATEPART('minute', [Disconnected At])),-1)-(ATTR(DATEPART('minute', [Connected At]))))
it isn't doing the trick.
Could someone help me to achieve my ideal response? Or similar result that would give me the completeness of date and time?
Thank You
Tableau ScreenShot
Use DATEDIFF by seconds between your two dates. Then create a calc field as follows:
//replace [Seconds] with whatever field has the number of seconds in it
//and use a custom number format of 00:00:00:00 (drop the first 0 to get rid of leading 0's for days)
IIF([Seconds] % 60 == 60,0,[Seconds] % 60)// seconds
+ IIF(INT([Seconds]/60) %60 == 60, 0, INT([Seconds]/60) %60) * 100 //minutes
+ IIF(INT([Seconds]/3600) % 24 == 0, 0, INT([Seconds]/3600) % 24) * 10000 //hours
+ INT([Seconds]/86400) * 1000000 // days
for more information, check out this blog post where I got this from. http://drawingwithnumbers.artisart.org/formatting-time-durations/

Compare dates in Lua

I have a variable with a date table that looks like this
* table:
[day]
* number: 15
[year]
* number: 2015
[month]
* number: 2
How do I get the days between the current date and the date above? Many thanks!
You can use os.time() to convert your table to seconds and get the current time and then use os.difftime() to compute the difference. see Lua Wiki for more details.
reference = os.time{day=15, year=2015, month=2}
daysfrom = os.difftime(os.time(), reference) / (24 * 60 * 60) -- seconds in a day
wholedays = math.floor(daysfrom)
print(wholedays) -- today it prints "1"
as #barnes53 pointed out could be off by one day for a few seconds so it's not ideal, but it may be good enough for your needs.
You can use the algorithms gathered here:
chrono-Compatible Low-Level Date Algorithms
The algorithms are shown using C++, but they can be easily implemented in Lua if you like, or you can implement them in C or C++ and then just provide Lua bindings.
The basic idea using these algorithms is to compute a day number for the two dates and then just subtract them to give you the number of days.
--[[
http://howardhinnant.github.io/date_algorithms.html
Returns number of days since civil 1970-01-01. Negative values indicate
days prior to 1970-01-01.
Preconditions: y-m-d represents a date in the civil (Gregorian) calendar
m is in [1, 12]
d is in [1, last_day_of_month(y, m)]
y is "approximately" in
[numeric_limits<Int>::min()/366, numeric_limits<Int>::max()/366]
Exact range of validity is:
[civil_from_days(numeric_limits<Int>::min()),
civil_from_days(numeric_limits<Int>::max()-719468)]
]]
function days_from_civil(y, m, d)
if m <= 2 then
y = y - 1
m = m + 9
else
m = m - 3
end
local era = math.floor(y/400)
local yoe = y - era * 400 -- [0, 399]
local doy = math.modf((153*m + 2)/5) + d-1 -- [0, 365]
local doe = yoe * 365 + math.modf(yoe/4) - math.modf(yoe/100) + doy -- [0, 146096]
return era * 146097 + doe - 719468
end
local reference_date = {year=2001, month = 1, day = 1}
local date = os.date("*t")
local reference_days = days_from_civil(reference_date.year, reference_date.month, reference_date.day)
local days = days_from_civil(date.year, date.month, date.day)
print(string.format("Today is %d days into the 21st century.",days-reference_days))
os.time (under Windows, at least) is limited to years from 1970 and up. If, for example, you need a general solution to also find ages in days for people born before 1970, this won't work. You can use a julian date conversion and subtract between the two numbers (today and your target date).
A sample julian date function that will work for practically any date AD is given below (Lua v5.3 because of // but you could adapt to earlier versions):
local
function div(n,d)
local a, b = 1, 1
if n < 0 then a = -1 end
if d < 0 then b = -1 end
return a * b * (math.abs(n) // math.abs(d))
end
--------------------------------------------------------------------------------
-- Convert a YYMMDD date to Julian since 1/1/1900 (negative answer possible)
--------------------------------------------------------------------------------
function julian(year, month, day)
local temp
if (year < 0) or (month < 1) or (month > 12)
or (day < 1) or (day > 31) then
return
end
temp = div(month - 14, 12)
return (
day - 32075 +
div(1461 * (year + 4800 + temp), 4) +
div(367 * (month - 2 - temp * 12), 12) -
div(3 * div(year + 4900 + temp, 100), 4)
) - 2415021
end

Use Lag function in SAS find difference and delete if the value is less than 30

Eg.
Subject Date
1 2/10/13
1 2/15/13
1 2/27/13
1 3/15/13
1 3/29/13
2 1/11/13
2 1/31/13
2 2/15/13
I would need only the subjects with the dates between them more than 30.
required output:
Subject Date
1 2/10/13
1 3/15/13
2 1/11/13
2 2/15/13
This is a very interesting problem. I'll use the retain statement in the DATA step.
Since we are trying to compare dates between different observations, it's a bit more difficult. We can take advantage of the fact that SAS can convert dates to SAS date values (i.e. number of days after Jan 1 1960). Then we can compare these numeric values using conditional statements.
data work.test;
input Subject Date anydtdte15.;
sasdate = Date;
retain x;
if -30 <= sasdate - x <= 30 then delete;
else x = sasdate;
datalines;
1 2/10/13
1 2/15/13
1 2/27/13
1 3/15/13
1 3/29/13
2 1/11/13
2 1/31/13
2 2/15/13
;
run;
proc print data=test;
format Date mmddyy8.;
var Subject Date;
run;
OUTPUT as required:
Obs Subject Date
1 1 02/10/13
2 1 03/15/13
3 2 01/11/13
4 2 02/15/13

Convert date to closest end-of-month date MATLAB

I need to convert a datenumber to its closest end-of-month date. I found an online link but it is very inefficient for a large matrix (at http://www.mathworks.com/matlabcentral/fileexchange/26374-round-off-dates-and-times). Does Matlab (Financial Toolbox) has an inbuilt function for this? I couldn't find it.
date_in = 734421 ;
somefunction(date_in) --> Sept 2010
Thanks!
Basically, it sounds like you are asking for whether a given date is closer to the preceding or following month. You can greatly simplify the logic involved if you use the functions EOMDAY to find the date for the end of the month and ADDTODATE to shift the current month up or down by one. Here's an example function that takes a date number as input:
function closestString = closest_month(dateNumber)
dateVector = datevec(dateNumber);
daysInMonth = eomday(dateVector(1),dateVector(2));
if dateVector(3) > daysInMonth/2
dateNumber = addtodate(dateNumber,1,'month');
else
dateNumber = addtodate(dateNumber,-1,'month');
end
closestString = datestr(dateNumber,'mmm yyyy');
end
I had some errors in my previous version. Here's the logic incorporated into a function. It also checks for the month and updates accordingly.
function out = roundMonth(dateNumber)
dateVector = datevec(dateNumber);
day = dateVector(3);
month = dateVector(2);
year = dateVector(1);
month = month + sign(day - 15 + double(~(month-2)))...
+ double(~(day-15 + double(~(month-2))));
dateVector(1) = year + double((month-12)==1) - double((1-month)==1);
dateVector(2) = mod(month,12) + 12*double(~mod(month,12));
out = datestr(dateVector,'mmm yyyy');
EXAMPLES:
1.
roundMonth(datenum('10-Oct-2010'))
ans =
Sep 2010
2.
roundMonth(datenum('20-Oct-2010'))
ans =
Nov 2010
3.
roundMonth(datenum('20-Dec-2010'))
ans =
Jan 2011
4.
roundMonth(datenum('10-Jan-2010'))
ans =
Dec 2009