How to use if/then to assign a year to a set of dates?
Ältere Kommentare anzeigen
I would like to add a new vector called "school_year" that assigns a year to each point. In this case, a school year will be considered as starting August 1 and ending the following year in July 31st. For example, August 1st, 2000 - July 31st, 2001 should be assigned as school year 2001.
I want to use an if/then based on the month/year to generate the "school_year" value for each data point. A hint I have been given in this assignment is to use an if/then statement, for example, if it is after July, then what?
So in this table, there would be a new column called school_year that should read the following:
2001
2001
2001
2001
2002
2002
2002
2002
2002
table_a = readtable('Data1.xlsx')
Akzeptierte Antwort
Weitere Antworten (1)
John D'Errico
am 6 Feb. 2023
Bearbeitet: John D'Errico
am 6 Feb. 2023
Easy peasy. :)
data = [8 7 2000 12
9 8 2000 14
9 9 2000 13
3 11 2001 11
8 3 2001 17
12 15 2001 14
2 2 2002 10
5 1 2002 9
7 3 2002 16];
You have year as the third column there. I'll call it trueyear. Just create a variable, call it schoolyear.
schoolyear = trueyear + (month >= 8);
Will that work? Of course. The school year is ALWAYS the same as the trueyear when you are in the first part of the year, so between January 1 and July 31. Once the month goes into month #8, this adds 1 to the trueyear.
There is no if statement required.
6 Kommentare
Macy
am 6 Feb. 2023
Walter Roberson
am 6 Feb. 2023
For variables such as month you would need to index according to the current row index.
You will find it easiest to generate all of the outputs first and add that entire output as a table variable, instead of assigning into the table as you go... unless, that is, you first generate a full-sized variable of default values.
Walter Roberson
am 7 Feb. 2023
table_a.school_year = schoolyear(:) ;
Macy
am 7 Feb. 2023
Kategorien
Mehr zu MATLAB finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!