Calculating CPI matrix and finding greatest change and yearly average help

6 ビュー (過去 30 日間)
I have an assigment asking this
1. The consumer price index (CPI) is a measure of the cost of goods. The example file called cpi.txt/dat contains the CPI for each month since January 2009. The information for a single year is contained in a single line with the year in YY format in the first location (13 columns). The first two lines of the file look like:
09 211.143 212.193 212.709 213.240 213.856 215.693 215.351 215.834 215.969 216.177 216.330 215.949
10 216.687 216.741 217.631 218.009 218.178 217.965 218.011 218.312 218.439 218.711 218.803 219.179
Write a program that will work for any data file that meets the format as given in cpi.txt/dat (13 columns with the year in column 1 as YY, and 12 months of CPI data for an unspecified number of years). Your program will:
a. Read this file into a matrix and store the years correctly as 20xx, where xx is the year number from the file.
b. Create a second matrix (cpipct) that replaces the CPI with the percent increase (decrease) from the previous month. Assume that the first month of the data set has a 0.00% change from the previous month.
c. Determine the month in each year that had the greatest magnitude percent change (positive or negative) in CPI using MATLAB built-in functions. Display the month number and the percent change to the command window for each year.
d. Calculate the average of the monthly increases in a given year. Determine the year with the maximum overall average of the monthly increases using MATLAB built-in functions. Display this information to the command window.
I have already solved A B,and C with this code
CPi=load('cpi.txt');
%makes the years into real years ex(09 to 2009)
CPi(:,1)=CPi(:,1)+2000;
%sets the array bounds
[x,y]=size(CPi);
%create new matrix for CPipct
for i=1:x
cpipct(i,1)=CPi(i,1);
cpipct(i,2)=0;
cpipct(i,3:13)=diff(CPi(i,2:13))./CPi(i,2:12)*100;
end
disp(cpipct);
year=cpipct(:,1);
%finding the months with the largest CPi change in a year
for i=1:x
percent=max(cpipct(i,2:end));
[X,Y]=find(abs(cpi(i,2:end))==max(abs(cpi(i,2:end))));
fprintf('For year %d, month %d had the greatest cpi change of %0.2f\n',year(i),Y(1)+1,percent);
end
for i=1:x
average=mean(cpipct(i,2:end));
fprintf('The average monthly increase for year %d, is %f\n',year(i),average);
end
I am however stuck on part D on finding the heighest average.(d. Calculate the average of the monthly increases in a given year. Determine the year with the maximum overall average of the monthly increases using MATLAB built-in functions) I got the average part down i just need help with the second part of the question.
Any advice or tips on how i should write the rest of this program to solve these two parts would be much appreciated. (I do not want full answer just a push in the right direction)
Below is the CPI text file if that helps
09 211.143 212.193 212.709 213.240 213.856 215.693 215.351 215.834 215.969 216.177 216.330 215.949
10 216.687 216.741 217.631 218.009 218.178 217.965 218.011 218.312 218.439 218.711 218.803 219.179
11 220.223 221.309 223.467 224.906 225.964 225.722 225.922 226.545 226.889 226.421 226.230 225.672
12 226.665 227.663 229.392 230.085 229.815 229.478 229.104 230.379 231.407 231.317 230.221 229.601
13 230.280 232.166 232.773 232.531 232.945 233.504 233.596 233.877 234.149 233.546 233.069 233.049
14 233.916 234.781 236.293 237.072 237.900 238.343 238.250 237.852 238.031 237.433 236.151 234.812
15 233.707 234.722 236.119 236.599 237.805 238.638 238.654 238.316 237.945 237.838 237.336 236.525
16 236.916 237.111 238.132 239.261 240.229 241.018 240.628 240.849 241.428 241.729 241.353 241.432
17 242.839 243.603 243.801 244.524 244.733 244.955 244.786 245.519 246.819 246.663 246.669 246.524
18 247.867 248.991 249.554 250.546 251.588 251.989 252.006 252.146 252.439 252.885 252.038 251.233
19 251.712 252.776 254.202 255.548 256.092 256.143 256.571 256.558 256.759 257.346 257.208 256.974
20 257.971 258.678 258.115 256.389 256.394 257.797 259.101 259.918 260.280 260.388 260.229 260.474

採用された回答

Aakash Garg
Aakash Garg 2021 年 3 月 30 日
編集済み: Aakash Garg 2021 年 3 月 30 日
You can try this-
for i=1:x
average(i)=mean(cpipct(i,2:end));
fprintf('The average monthly increase for year %d, is %f\n',year(i),average(i));
end
[M,idx] = max(average);
fprintf('Answer - %d',year(idx));
% if two years have same max average monthly increase then the output will be the year with lower index in the vector
% eg: Assume 100 is the max value.
%
% average(7) = 100;
% average(11) = 100;
% [M,idx] = max(average);
%
%
% M will be 100 and idx will be 7

その他の回答 (0 件)

Community Treasure Hunt

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

Start Hunting!

Translated by