Running average using for loops

I am tasked with writing a code which calculating a running average using a for loop on any desired file with any number of 'n' - samples and plot it against the raw data. This is how far I've managed to get, it runs with samples of 1 but everything above that it throws an error on lines 3 and 22. Any guidance is greatly appreciated.
edit: I've attached the file of the data I'm attempting to average and added the error messages I recieved.
%number of samples
n = input('enter sample amount: ');
%file of data
s = input('enter file name: ','s');
%function name
b = run_avg(n,s);
a = load(s);
%plot of raw data against the averaged data
plot(1:length(b),b, 1:length(a),a);
function avg = run_avg(n,s)
a = load(s);
x = length(a);
b = zeros(1,x);
%for loop to filter data of uploaded file.
for i = 1:x
%for the beginning of data set
if i < n-2
for j= i:i+(n-1)
b(i) = b(i) + a(j);
end
end
if i > n-2
for j = i - (n-1)/2 : i + (n-1)/2
b(i) = b(i) + a(j);
end
end
end
avg = b/n;
end
%error message
Array indices must be positive integers or logical values.
Error in run_av6>run_avg (line 22)
b(i) = b(i) + a(j);
Error in run_av6 (line 3)
b = run_avg(n,s);

5 Comments

Your code doesn't contain any comments, nor are the variable names very descriptive. We also don't have the files, so we can't run your code. We don't know what error occurred either. Try to make a MWE so we can run your code without any other dependencies and can reproduce your issue.
Rik
Rik on 13 Mar 2019
Edited: Rik on 13 Mar 2019
So for your homework you need to use a for loop. Is the output equivalent to a convolution? Then we would at least know for sure the goal of your code.
You still haven't provided a MWE, so I can't reproduce the error. It looks like it shouldn't occur, but I can view the state of the other variables.
I also don't understand your code. There are some comments, but they don't provide enough information. Your nested loops have no comment about what it is intended to do. I also don't understand why you choose to load s twice, instead of loading it once and then passing it as an argument. Shouldn't a window avering function only have a data input and a window size input?
I don't know what an MWE is, I tried searching but haven't found what you mean. I loaded s twice because I thought I had to in order to plot it correctly then to run the function correctly. The loop is intended to calculate running average on the data of a file based on a sample size. I'm trying to account for the begining and end of the data sat as well as everything in between.
In my first comment I linked to the Wikipedia page for minimal working example. So you need to provide a resonable value for n, as well as a reasonable value for s. Once you have provided those we can help you further.
As for you code itself, it is still unclear to me what you are doing. Try to explain why you have two inner loops separated by some logic. Why are you using those logical statements? What do they mean? Try to write a comment for each line.
The way I would write a window average is either with a convolution, or with a single loop.
Thank you for your guidance. I scrapped the majority of what I wrote and used your advice for a window average combined with a convolution along with the code you provided. Again I am incredibly grateful for your patience and consistency in helpin me figure this out.

Sign in to comment.

 Accepted Answer

I guess boldly:
If you want a sliding window average, you can use a convolution:
data=magic(8);data=data(:);%generate some data
x=linspace(0,1,numel(data));%generate some x-values
%create the filter kernel
window_kernel=[1 1 1 1];window_kernel=window_kernel/sum(window_kernel);
%apply convolution
new_data=conv(data,window_kernel,'same');
%plot
figure(1),clf(1)
plot(x,data,'r','DisplayName','raw')
hold on
plot(x,new_data,'b','DisplayName','filtered')
hold off
legend

1 Comment

Thank you but I need the average to be calculate through a for loop, I realize I didnt state this in the question sorry for the miscommunication.

Sign in to comment.

More Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Asked:

on 13 Mar 2019

Commented:

on 15 Mar 2019

Community Treasure Hunt

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

Start Hunting!