Running average using for loops
Show older comments
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
Rik
on 13 Mar 2019
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.
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?
Matthew Healy
on 13 Mar 2019
Rik
on 14 Mar 2019
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.
Matthew Healy
on 15 Mar 2019
Accepted Answer
More Answers (0)
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!