How to modify an array's elements using another element?
显示 更早的评论
I need to write a for loop that will set each array element in bonusScore to the sum of itself and the next element (except for the last element which stays the same). Why why does my code yield these (incorrect) output numbers? What should I do to fix my code?
function bonusScores = CombineScores(numberScores, userScores)
for i = 1:numbersScores;
userScores(1) = userScores(1) .* 2 + userScores(2);
userScores(2) = userScores(2) .* 2 + userScores(3);
userScores(3) = userScores(3) .* 2 + userScores(4);
bonusScores = userScores;
end
end
Input: CombineScores(4, [10, 20, 30, 40]) Output: ans = 1800, 1960, 1080, 40
2 个评论
KALYAN ACHARJYA
2018-10-20
编辑:KALYAN ACHARJYA
2018-10-20
What results you are expecting on above-mentioned input?
Input: CombineScores(4, [10, 20, 30, 40])
Matthew Burton
2018-10-21
采纳的回答
更多回答(0 个)
类别
在 帮助中心 和 File Exchange 中查找有关 Loops and Conditional Statements 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!