Using symsum within a loop
10 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Fatima Ramadan
el 15 de Oct. de 2019
Comentada: Fatima Ramadan
el 15 de Oct. de 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 ?
0 comentarios
Respuesta aceptada
Walter Roberson
el 15 de Oct. de 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.
Más respuestas (0)
Ver también
Categorías
Más información sobre Calculus en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!