Find the Monday preceding the third Friday of the month

8 visualizaciones (últimos 30 días)
Cary
Cary el 13 de Jul. de 2015
Comentada: Sean de Wolski el 16 de Jul. de 2015
Ok, I have data spanning numerous years, and part of my analysis requires me to know the Monday that precedes the third Friday of every month. Right now I am stuck on just calculating the third Friday of the month.
tdayStr=datestr(datenum(num2str(vifDate),'yyyymmdd')); % turn vifDate into string
dt=datetime(tdayStr); % get datetime format to extrapolate day of week
m=month(dt); % get month of each date
y=year(dt); % get year of each date
dayNumber=day(dt,'dayofweek'); % Friday=6
x=zeros(81,1); % preallocate a vector and find the Fridays of each month/year
yUnique=unique(y);
mUnique=unique(m);
for i = yUnique(1:end);
for j = mUnique(1,end);
f=find(j==mUnique & dayNumber==6);
%x(i)=f(3);
end
end
The code breaks down at "f=find(j==mUnique & dayNumber==6);", and the error says inputs must have the same size. How can I troubleshoot this error, and more importantly, after finding the third Friday of every month, how can I get the preceding Monday?
Thank you for reading.

Respuesta aceptada

Sean de Wolski
Sean de Wolski el 13 de Jul. de 2015
Editada: Sean de Wolski el 13 de Jul. de 2015
If you have the Financial Toolbox, you can do this with nweekdate which will do both aspects, finding third Friday, and finding the Monday in the same week as the third Friday.
More
Without Financial Tbx, this can be done with some grouping. Here is an example for 2015:
% 2015
Time = (datetime(2015,1,1):days(1):datetime(2015,12,31)).';
% Day of Month and whether it's a Friday
mt = month(Time);
idxFriday = strcmp(day(Time,'shortname'),'Fri');
% One to number of days
oneToN = (1:numel(Time))';
% Group Fridays by month
Fridays = accumarray(mt(idxFriday),oneToN(idxFriday),[],@(x){sort(x)});
% Keep third index and subtract four days for Monday
idxMonday = cellfun(@(x)x(3),Fridays)-4;
% Extract from Time
Time(idxTime)
Yields:
12-Jan-2015
16-Feb-2015
16-Mar-2015
13-Apr-2015
11-May-2015
15-Jun-2015
13-Jul-2015
17-Aug-2015
14-Sep-2015
12-Oct-2015
16-Nov-2015
14-Dec-2015
  6 comentarios
Cary
Cary el 13 de Jul. de 2015
Thanks! :)
Sean de Wolski
Sean de Wolski el 16 de Jul. de 2015
For anyone reading this in the future, Peter's answer is much better.

Iniciar sesión para comentar.

Más respuestas (1)

Peter Perkins
Peter Perkins el 13 de Jul. de 2015
In R2014b or later, using datetime, this is simple:
% Generate some random dates
>> d = sort(datetime('now','Format','eee, dd-MMM-yyyy') + caldays(randi(300,5,1)))
d =
Thu, 03-Sep-2015
Sun, 08-Nov-2015
Tue, 26-Jan-2016
Sun, 21-Feb-2016
Fri, 26-Feb-2016
% Get the first of the month
>> d1 = dateshift(d,'start','month')
d1 =
Tue, 01-Sep-2015
Sun, 01-Nov-2015
Fri, 01-Jan-2016
Mon, 01-Feb-2016
Mon, 01-Feb-2016
% Get the 3rd Friday
>> d2 = dateshift(d1,'dayofweek','friday',3)
d2 =
Fri, 18-Sep-2015
Fri, 20-Nov-2015
Fri, 15-Jan-2016
Fri, 19-Feb-2016
Fri, 19-Feb-2016
% Get the previous Monday
>> d3 = d2 - caldays(4)
d3 =
Mon, 14-Sep-2015
Mon, 16-Nov-2015
Mon, 11-Jan-2016
Mon, 15-Feb-2016
Mon, 15-Feb-2016
I'm not 100% sure what your vifDate variable contains, but it looks like it's "packed decimal" values. Try this to turn them into datetimes:
>> datetime(20150713,'ConvertFrom','yyyymmdd')
ans =
13-Jul-2015 00:00:00
Hope this helps.

Categorías

Más información sobre Dates and Time en Help Center y File Exchange.

Etiquetas

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by