Using symsum within a loop

10 次查看(过去 30 天)
hello, i am trying to implement a moving average for a recording 'rec' by taking 3 consecutive data points at each iteration and averaging them. i tried to use symsum in my code below but i keep getting an error:
error using sym/subsindex (line 737)
Invalid indexing or function definition. When defining a function, ensure that the arguments are symbolic variables and the body of the function is a SYM expression. When indexing, the input must be numeric, logical, or ':'.
here is my code:
%%%%%
for i=2:length(rec) -1
syms k
filtered_rec(i) = symsum(rec(i+k),k,(m-1)/2,(m+1)/2);
end
%%%%%
Any ideas,thanks ?

采纳的回答

Walter Roberson
Walter Roberson 2019-10-15
symbolic variables can never be used as indices, including not in symsum.
You should just construct the terms and sum() them
sum( rec(i+(m-1)/2:i+(m+1)/2) )
Or you could skip all of the looping and symbolic variables and just use
filtered_rec = rec(2:end-1) + rec(3:end);
This makes assumptions about the value of m. However, if those assumptions are not justified, then rec(i+(m-1)/2) would be out of range considering that you are going to length(rec)-1 on the for loop.
  1 个评论
Fatima Ramadan
Fatima Ramadan 2019-10-15
thanks,that was way simpler! i'll just make sure the summation terms are not out of range.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Calculus 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by