Using if-else statement to determine which value to input into function
Ältere Kommentare anzeigen
Hello,
I am trying to calculate final grades from the data given in the screenshot. From left to right columns are
1) student ID
2) test 1 grade
3) test 2 grade
4) midterm grade
5) project grade
6) final grade
Tests are worth 10% and whichever test is higher is used in final calculation.
I am using the following to calculate final grades:
if samplegrades(:,2) > samplegrades(:,3)
t = samplegrades(:,2)
else
t = samplegrades(:,3)
end
m = samplegrades(:,4)
p = samplegrades(:,5)
f = samplegrades(:,6)
finalgrades = @(t,m,p,f) (t.*.10+m.*.20 + p.*.20+f.*.50)
finalgrades(t,m,p,f)
The problem is that the if-else statement doesn't seem to be using the higher t value. It is only using values from test2 to determine final grade. Please help if you can.
1 Kommentar
dpb
am 13 Jun. 2022
Read the doc for if, elseif, else carefully; particularly the part that explains how MATLAB determines what is TRUE.
Then see <find-array-elements-that-meet-a-condition> for introduction to logical addressing. It's possible to write the desired code w/o a for...end loop, but many beginners find that the easiest first.
Since this is a homework problem, we'll coach and let you figure out the next steps instead of just handing the solution to you...
Akzeptierte Antwort
Weitere Antworten (0)
Kategorien
Mehr zu Performance and Memory 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!