Writing a loop instead of sum

1 回表示 (過去 30 日間)
Fred
Fred 2012 年 10 月 3 日
Hello,
n=40; b=1; m=6; s=0.15
for k=1:(n-(2*b)-m+1)
c=L(k+1:k+m-1);
if sum(abs(c-L(k)))<=2*s %How can I use a loop instead of this sum
lam_d=(1/m)*sum(L(k:k+m-1));
break
end
end
I have problem with line starting with sum, can anybody tell me how can I use a loop instead of sum?
Thank you

採用された回答

Image Analyst
Image Analyst 2012 年 10 月 4 日
In general, to sum up a value in a loop
theSum = 0;
for k = 1 : whatever
theSum = theSum + theVectorValues(k);
end
I don't know why you can't just use sum(), but just adapt this into your code. Be careful not to call your sum "sum" or you'll blow away the built-in sum() function.
  1 件のコメント
Fred
Fred 2012 年 10 月 4 日
Thank you, I realized that I had used sum in other lines as a parameter so it was the making problem :)

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

タグ

製品

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by