Using symsum within a loop

10 visualizzazioni (ultimi 30 giorni)
Fatima Ramadan
Fatima Ramadan il 15 Ott 2019
Commentato: Fatima Ramadan il 15 Ott 2019
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 ?

Risposta accettata

Walter Roberson
Walter Roberson il 15 Ott 2019
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 Commento
Fatima Ramadan
Fatima Ramadan il 15 Ott 2019
thanks,that was way simpler! i'll just make sure the summation terms are not out of range.

Accedi per commentare.

Più risposte (0)

Community Treasure Hunt

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

Start Hunting!

Translated by