MATLAB: How to create a vector with the first day of each month vector;months;first;date How do I create a column vector with the first day of each month? Considering I have a list of days… Best Answer Not quite sure what you are asking for here.I assume you have a vector with all days from Monday-Sunday, and you know which day the first day of the year is:weekdays = [{'Monday'},{'Tuesday'},{'Wednesday'},{'Thursday'},{'Friday'},{'Saturday'},{'Sunday'}];dayNumber = zeros(1,12);dayNumber(1) = 2; % 2013 started on a Tuesday.numDays = [31 28 31 30 31 30 31 31 30 31 30 31]; % Not a leap yearfor i = 1:11 dayNumber(i+1) = mod((dayNumber(i)+days(i)-1),7)+1; endfor i = 1:12 dayName(i) = weekdays(dayNumber(i));endIf you need to account for leap years: It is a leap year every fourth year, except years that can be divided by 100, except years that can be divided by 400.A general approach would be something like this:year = 2013;year0 = 1900;startDay = 1; % Year 1900 started on a monday.if year > year0 for i = 1:(year-year0) if mod((year0+i),4)~=0 startDay = startDay + 365; elseif mod((year0+i),4)==0 && mod((year0+i),100)~=0 startDay = startDay + 366; elseif mod((year0+i),4)==0 && mod((year0+i),100)==0 && mod((year0+i),400)~=0 startDay = startDay + 365; elseif mod((year0+i),4)==1 && mod((year0+i),400)==0 startDay = startDay + 366; end endenddayNumber = mod(startDay,7)+1;This gives dayNumber = 2, for year = 2013. Then continue with the code above. (numDays(2)=29, if leap year) Related SolutionsMATLAB: Program not running correctly. Trying to write program to determine number of days in month. ...switch (month) case {1,3,5,7,8,10,12} days = 31; case {4,6,9,11} days =30; case 2 days=28 + leap_year;endBTW, trytype eomdayat command line... :) MATLAB: Function that transforms years into days Like this?function numDay = year2days(year) numDay = days(datetime(year,12,31)-datetime(year,1,1))+1;endThe following is one example:>> year2days(2000)ans = 366 Related QuestionChange the number of the day since specified year to dateConvert to a functionHow to easily get for each day in 2013 the type of day? ( Weekday, Saturday, Sunday)Convert hour, minute, and day to fraction of a day
Best Answer