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!