How to solve error of index exceeds number of array elements?

1 view (last 30 days)
Hi, I am using MATLAB R2020a on MacOS. I am trying to find the exponentially weighted moving mean of my signal 'cycle_periods' using the dsp.MovingAverage function algorithm. I am trying to also remove outliers on an element by element basis in real-time. However, I keep getting this error:
% Calculate successive weights to test how they change with different FF
% values
lambda = 0.1;
w = zeros(length(cycle_periods),1);
w(1) = 1; % initialize the weight for the first sample
for i = 2:length(cycle_periods)
w(i) = lambda*w(i-1) + 1; % calculate the successive weights
end
% Calculate moving mean with weights manually
x = zeros(length(cycle_periods), 1);
x(1) = cycle_periods(1);
for i = 2:length(cycle_periods)
x(i) = (1 - 1/w(i))*x(i - 1) + (1/w(i))*cycle_periods(i);
if x(i) > 1.5*x(i - 1) % Remove high outliers
x(i) = [];
elseif x(i) < 0.5*x(i - 1) % Remove low outliers
x(i) = [];
end
end
Index exceeds the number of array elements (45).
Error in R09_11_20 (line 86)
x(i) = (1 - 1/w(i))*x(i - 1) + (1/w(i))*cycle_periods(i);
Any help would be much appreciated, thanks!

Answers (0)

Products


Release

R2020a

Community Treasure Hunt

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

Start Hunting!