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')
table_a = 9×4 table
month day year students _____ ___ ____ ________ 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

 採用された回答

Les Beckham
Les Beckham 2023 年 2 月 6 日
編集済み: Les Beckham 2023 年 2 月 6 日

0 投票

table_a = readtable('https://www.mathworks.com/matlabcentral/answers/uploaded_files/1286455/Data1.xlsx')
table_a = 9×4 table
month day year students _____ ___ ____ ________ 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
table_a.date = datetime(table_a.year, table_a.month, table_a.day)
table_a = 9×5 table
month day year students date _____ ___ ____ ________ ___________ 8 7 2000 12 07-Aug-2000 9 8 2000 14 08-Sep-2000 9 9 2000 13 09-Sep-2000 3 11 2001 11 11-Mar-2001 8 3 2001 17 03-Aug-2001 12 15 2001 14 15-Dec-2001 2 2 2002 10 02-Feb-2002 5 1 2002 9 01-May-2002 7 3 2002 16 03-Jul-2002
schoolyear = zeros(size(table_a.date));
schoolyear(isbetween(table_a.date, '01-Aug-2000', '31-Jul-2001')) = 2001;
schoolyear(isbetween(table_a.date, '01-Aug-2001', '31-Jul-2002')) = 2002;
table_a.schoolyear = schoolyear
table_a = 9×6 table
month day year students date schoolyear _____ ___ ____ ________ ___________ __________ 8 7 2000 12 07-Aug-2000 2001 9 8 2000 14 08-Sep-2000 2001 9 9 2000 13 09-Sep-2000 2001 3 11 2001 11 11-Mar-2001 2001 8 3 2001 17 03-Aug-2001 2002 12 15 2001 14 15-Dec-2001 2002 2 2 2002 10 02-Feb-2002 2002 5 1 2002 9 01-May-2002 2002 7 3 2002 16 03-Jul-2002 2002
If you have more data than just this small sample, the hard-coded statements above that set the schoolyear could be replaced with code like this:
found_years = unique(table_a.year);
for i = 1:numel(found_years)
schoolyear(isbetween(table_a.date, sprintf('01-Aug-%d', found_years(i)-1), sprintf('31-Jul-%d', found_years(i)))) = found_years(i);
end
schoolyear
schoolyear = 9×1
2001 2001 2001 2001 2002 2002 2002 2002 2002

6 件のコメント

Macy
Macy 2023 年 2 月 6 日
Thank you! Yes this is a sample set of data a part of a much larger set. How can this be altered to use the month and year vectors and not make the "date" vector. So if it's after July, then ?
This was reccomended to me by my professor to split the date into 3 columns and use them in the if/then, although your method works great too.
Les Beckham
Les Beckham 2023 年 2 月 6 日
編集済み: Les Beckham 2023 年 2 月 6 日
John's answer is simpler than mine and doesn't require creating the date vector or any loop.
table_a = readtable('https://www.mathworks.com/matlabcentral/answers/uploaded_files/1286455/Data1.xlsx')
table_a = 9×4 table
month day year students _____ ___ ____ ________ 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
table_a.schoolyear = table_a.year + (table_a.month >= 8)
table_a = 9×5 table
month day year students schoolyear _____ ___ ____ ________ __________ 8 7 2000 12 2001 9 8 2000 14 2001 9 9 2000 13 2001 3 11 2001 11 2001 8 3 2001 17 2002 12 15 2001 14 2002 2 2 2002 10 2002 5 1 2002 9 2002 7 3 2002 16 2002
Macy
Macy 2023 年 2 月 6 日
Ah I see ok, thank you. I want to use an if/then just to see how it would work (reccomended by my professor to try it out).
I'm trying to write it out like so, but I don't really know how to code. Is this even the right idea when my prof says to use an if/then?
for i = 1:??
if table_a.month>=8
schoolyear = table_a.year + 1
elseif table_a.month<7
schoolyear = year
end
end
schoolyear
I'm also not sure how to creat the schoolyear vector in the first place.
Les Beckham
Les Beckham 2023 年 2 月 7 日
編集済み: Les Beckham 2023 年 2 月 7 日
That's pretty close. With a few changes, your code will work. Obviously, the version without the loop and if test is simpler and takes advantage of the ability of Matlab to operate on vectors and matrices all at once (hence the name Matlab for matrix laboratory).
table_a = readtable('https://www.mathworks.com/matlabcentral/answers/uploaded_files/1286455/Data1.xlsx')
table_a = 9×4 table
month day year students _____ ___ ____ ________ 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
schoolyear = zeros(size(table_a.month)); % pre-allocate a vector the same size as table_a.month
for i = 1:numel(table_a.month) % numel tells how many elements (months) there are
if table_a.month(i) >= 8 % address individual elements using the loop index (i)
schoolyear(i) = table_a.year(i) + 1;
else % don't need the elseif, just use else
schoolyear(i) = table_a.year(i);
end
end
table_a.schoolyear = schoolyear
table_a = 9×5 table
month day year students schoolyear _____ ___ ____ ________ __________ 8 7 2000 12 2001 9 8 2000 14 2001 9 9 2000 13 2001 3 11 2001 11 2001 8 3 2001 17 2002 12 15 2001 14 2002 2 2 2002 10 2002 5 1 2002 9 2002 7 3 2002 16 2002
Macy
Macy 2023 年 2 月 7 日
Thank you @Les Beckham for explaining that in depth, I learned many new functions, and I will include both methods in my assignment!
Les Beckham
Les Beckham 2023 年 2 月 7 日
You are quite welcome.

サインインしてコメントする。

その他の回答 (1 件)

John D'Errico
John D'Errico 2023 年 2 月 6 日
編集済み: John D'Errico 2023 年 2 月 6 日

2 投票

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 件のコメント

Macy
Macy 2023 年 2 月 6 日
Nice, this is a very smart way to think about it!
For my assignment this gets the job done, although do you know how I could do it using the if statement just so I can see how it's done, as the hint we were given in the assignment was to use an if statement, although as I see here it's not required to accomplish the same task.
Macy
Macy 2023 年 2 月 6 日
編集済み: Macy 2023 年 2 月 6 日
Like using the same idea I am thinking of this, but I don't know how to code it properly (I am very new to matlab).
for i = 1:??
if month>=8
schoolyear = year + 1
elseif month<8
schoolyear = year
end
end
Walter Roberson
Walter Roberson 2023 年 2 月 6 日
You would want to loop over the valid row indices into the table; see height
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.
Macy
Macy 2023 年 2 月 7 日
編集済み: Macy 2023 年 2 月 7 日
Got it, thank you @Walter Roberson. I generated the outputs first like you suggested and got the code below. In this code, is school year vector? How can I make it a column in a matrix?
for i = 1:table_a.month
if table_a.month(i)>=8
schoolyear(i) = table_a.year(i) + 1;
else
schoolyear(i) = table_a.year(i);
end
end
schoolyear
Walter Roberson
Walter Roberson 2023 年 2 月 7 日
table_a.school_year = schoolyear(:) ;
Macy
Macy 2023 年 2 月 7 日
@Walter Roberson Thank you so much :)

サインインしてコメントする。

カテゴリ

製品

リリース

R2022b

質問済み:

2023 年 2 月 6 日

コメント済み:

2023 年 2 月 7 日

Community Treasure Hunt

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

Start Hunting!

Translated by