MATLAB: Using symsum within a loop

moving averagesymsum

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 ?

Best Answer

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.