Calculating combinations of dates

Dear all,
I have the following cell vector of dates
jji= '23/11/08'
'28/12/08'
'25/01/09'
that corresponds to the cell vector of values
values
[0.5637]
[0.6034]
[0.5943]
[0.4567 ]
and I want to create the following new vector of values
(23*0.5637+7*0.6034)/30
(28*0.6034+3*0.5943)/31
(25*5943+6*0.4567)/31
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
'23/11/08-30/11/08
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'
'28/12/08'
'25/01/09'
};
values ={
0.5637
0.6034
0.5943
0.4567 };
for i = 1:length(jji)
jjii{i,1} = jji{i,1}(1:2);
end
that produces
jjii =
'23'
'28'
'25'
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

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?
THanks

Sign in to comment.

 Accepted Answer

Andrei Bobrov
Andrei Bobrov on 30 Jun 2012
Edited: Andrei Bobrov on 30 Jun 2012
EDIT
[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;

5 Comments

Thanks andrei very much
I did as you said
jji={ '23/11/08'
'28/12/08'
'25/01/09'};
values ={
0.5637
0.6034
0.5943
0.4567 };
[Y M D] = datevec(jji,'dd/mm/yyyy');
ED = eomday(Y, M);
n = numel(values);
out = sum([D, ED-D].*values(hankel(1:n-1,n+[-1 0])),2)./ED;
but I get the warning
Undefined function or method 'times' for input arguments of type 'cell'.
Do I need to install any function?
thanks
I did a small change and it seems to be ok now
clear all clc
jji={ '23/11/08'
'28/12/08'
'25/01/09'};
values ={
0.5637
0.6034
0.5943
0.4567 };
[Y M D] = datevec(jji,'dd/mm/yyyy');
ED = eomday(Y, M);
n = numel(values);
awe=cell2mat(values(hankel(1:n-1,n+[-1 0])))
out = sum([D, ED-D].*awe,2)./ED;
could you please verify that my correction is correct?
thanks
In any case, I have to admit that you are really good in Matlab programing!
Sorry! I am use values as double array:
values =[0.5637
0.6034
0.5943
0.4567];
See EDIT
Thanks Andrei.
I would like to ask what numel does in : n= numel(values);
Could we alternative have
kk=size(values);
n=kk(1,1);
thank you

Sign in to comment.

More Answers (0)

Categories

Find more on MATLAB in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!