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 ?
0 个评论
采纳的回答
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.
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Calculus 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!