Matlab: Using calculating with days. My problem is the following:
I have a start day. For example: 30 - Oct - 2010
And I know how many business days I need to count back. For example: 250 business days.
Now I want to be able to calculate the start date.
Start date = (30 - Oct - 2010) - (250 Business Days) - (Amount of weekends during those 250  days).
But I don't know how to calculate the "amount of weekends".
Could anyone help me?
Thanks in advance.
Jonathan
 A: You can do it this way in Matlab:
% Data:
count_back = 250;
start_day = datenum('31-Oct-2013');

% Computations:
start_weekday = mod(start_day+4,7);
aux = cumsum(mod(start_weekday-1:-1:start_weekday-max(2*count_back,3),7) < 5);
days_back = find(aux==count_back, 1);
solution_date = datestr(start_day - days_back)

Comments: 


*

*In the first line of computations, a 0 in start_weekday indicates that start_day is Mon; 1 indicates that it is Thu etc. The +4 is needed because the reference date of the datenum function is 01-Jan-0000.

*The comparison <5 in the second line gives 1 for business days and 0 for weekends. The function cumsum gives the cumulative sum from yesterday backwards, reaching until max(2*count_back,3) days into the past. The number max(2*count_back,3) is always enough (it could be adjusted more finely).

*The 1 in the third line is needed because we want the first day into the past that gives the desired count_back. If that day is Mon, the previous Sun and Sat would also have the desired count_back, but we don't want them.

