Calculating combinations of dates

4 views (last 30 days)
antonet on 30 Jun 2012
Dear all,
I have the following cell vector of dates
jji= '23/11/08'
that corresponds to the cell vector of values
[0.4567 ]
and I want to create the following new vector of values
where in the first row of this vector the numerator (23*0.5637+7*0.6034) is the sum of two products.
In the first product which is 23*0.5637 the number 23 is actually the first two digits from '23/11/08'
In the second product which is 7*0.6034 the number 7 is actually the distance in days between '23/11/08' and the end date of that month, that is the difference
The denominator in the first row (30) is the total number of days for month November 2008
Similar analysis holds for the rest of the rows.
In my case I have a huge vector of dates and values. is there any way to construct the new vector of values?
My proposal is this:
jji={ '23/11/08'
values ={
0.4567 };
for i = 1:length(jji)
jjii{i,1} = jji{i,1}(1:2);
that produces
jjii =
TO find the end dates of each month i use
[Y, M] = datevec(jji,'dd/mm/yy'); enddates = datestr(datenum(Y,M+1,1)-1,'dd/mm/yy')
and I obtain
30/11/08 31/12/08 31/01/09
but then I need to calculate the differences between the jji and "endndates"
Any suggestions?
thank you
  1 Comment
antonet on 30 Jun 2012
I think I found how to calculate this difference
datenum(enddates,'dd/mm/yy') - datenum(jji,'dd/mm/yy')
But is there a better approach?

Sign in to comment.

Accepted Answer

Andrei Bobrov
Andrei Bobrov on 30 Jun 2012
Edited: Andrei Bobrov on 30 Jun 2012
[Y M D] = datevec(jji,'dd/mm/yyyy');
ED = eomday(Y, M);
v = cell2mat(values);
n = numel(v);
out = sum([D, ED-D].*v(hankel(1:n-1,n+[-1 0])),2)./ED;
antonet on 30 Jun 2012
Thanks Andrei.
I would like to ask what numel does in : n= numel(values);
Could we alternative have
thank you

Sign in to comment.

More Answers (0)


Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by