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!