I have written the code below and it successfully prints out the average that I want PTS/G for all 20 values, however the matrix that is printed out has 19 extra columns of 0s. How do I delete those?
%average points per season
%ave = (PTS/G)
PTS =[539;1220;996;1485;1938;2019;2461;1557;1819;2832;2430;2323;2201;1970;2078;1616;2133;83;782;1161]
G = [71;79;50;66;68;80;82;65;66;80;77;82;82;73;82;58;78;6;35;66]
for ii=1:20
ave(ii)= PTS(ii)/G(ii)
disp(PTS(ii)/G(ii))
end

 Réponse acceptée

Tommy
Tommy le 6 Avr 2020

0 votes

The extra columns may be left over from running
ave = (PTS/G)
If you clear your workspace, they should go away. However, if you want to divide each value in PTS by the corresponding value in G, you can just use
ave = PTS./G

6 commentaires

collegestudent
collegestudent le 6 Avr 2020
And if I want to add the calculated ave values to have a year appear beside them increasing each time, how would I do that?
Tommy
Tommy le 6 Avr 2020
Modifié(e) : Tommy le 6 Avr 2020
Do you mean something like this?
PTS = [539;1220;996;1485;1938;2019;2461;1557;1819;2832;2430;2323;2201;1970;2078;1616;2133;83;782;1161];
G = [71;79;50;66;68;80;82;65;66;80;77;82;82;73;82;58;78;6;35;66];
ave = PTS./G;
yrs = (2000:2019)';
fprintf('%.2f avg points per game in %i\n', [ave yrs]')
collegestudent
collegestudent le 6 Avr 2020
Yes! Exactly that, but including the for loop from before. Is that possible?
Sure, maybe this?
PTS = [539;1220;996;1485;1938;2019;2461;1557;1819;2832;2430;2323;2201;1970;2078;1616;2133;83;782;1161];
G = [71;79;50;66;68;80;82;65;66;80;77;82;82;73;82;58;78;6;35;66];
yrs = (2000:2019)';
for ii = 1:numel(PTS)
fprintf('%.2f avg points per game in %i\n', PTS(ii)/G(ii), yrs(ii))
end
If these are soccer games, I'm impressed!
collegestudent
collegestudent le 6 Avr 2020
This worked great thank you so much for getting back to me so quicky! No I wish it was soccer, it is Kobe's stats through his seasons :)
Tommy
Tommy le 6 Avr 2020
Ah :'(
But happy to help!

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Just for fun dans Centre d'aide et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by