Using if-else statement to determine which value to input into function
显示 更早的评论
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 个评论
dpb
2022-6-13
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...
采纳的回答
更多回答(0 个)
类别
在 帮助中心 和 File Exchange 中查找有关 Performance and Memory 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!