Azzera filtri
Azzera filtri

Find the Monday preceding the third Friday of the month

4 visualizzazioni (ultimi 30 giorni)
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
for i = yUnique(1:end);
for j = mUnique(1,end);
f=find(j==mUnique & dayNumber==6);
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.

Risposta accettata

Sean de Wolski
Sean de Wolski il 13 Lug 2015
Modificato: Sean de Wolski il 13 Lug 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.
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
  6 Commenti
Sean de Wolski
Sean de Wolski il 16 Lug 2015
For anyone reading this in the future, Peter's answer is much better.

Accedi per commentare.

Più risposte (1)

Peter Perkins
Peter Perkins il 13 Lug 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.


Scopri di più su Dates and Time in Help Center e File Exchange


Community Treasure Hunt

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

Start Hunting!

Translated by