trouble with finding sum in a for loop
1 次查看(过去 30 天)
显示 更早的评论
I have a for loop that is supposed to find the sum of the vector. It works with most vectors except those with zeros, please help. the specific input i have trouble with is
an2 = findSum([5 5 5 0 0])
function [sum] = findSum(vector)
sum = 0;
a = 1:20;
for i = vector
sum = sum + a(i);
end
end
1 个评论
Dyuman Joshi
2022-10-20
(Assuming it is necessary for you to use the for loop)
You want to calculate the sum of a vector, but why are you not using that vector or its elements for the operation?
采纳的回答
更多回答(0 个)
另请参阅
类别
在 Help Center 和 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!