code returns multiple "ans" instead of array

im trying to write a code to perfom a simple moving average on a input array but the output give multiple ans' instead of one array.
function avrgdata = Movingaverage(data,windsz)
n = [zeros(1,windsz-1),data];
avrgdata = zeros(length(data));
for i=1:length(data)
avrgdata(1,i)= (sum(n(i:i+windsz-1)))/windsz;
end

8 件のコメント

Adam
Adam 2019 年 10 月 25 日
What is the purpose of this:
avrgdata=preavrgdata;
You declared preavrgdata correctly to store results in a loop via indexing, but this line would just assign the final element of the loop to the variable you actually return (which should still just be a single answer, though not what you want).
Why can't you just return preavrgdata instead?
Also the 'return' command is redundant.
Guillaume
Guillaume 2019 年 10 月 25 日
To add to Adam's comment, what is this meant to be doing:
for i=1:size(data):1
The size(data) increment (which is a vector) is most likely a bug. In any case, this loop will only iterate once since it starts and ends at 1.
Why can't you use movmean to calculate a moving average?
Oluremi Falowo
Oluremi Falowo 2019 年 10 月 25 日
ok i have updated the code
function avrgdata = Movingaverage(data,windsz)
n = [zeros(1,windsz-1),data];
avrgdata = zeros([1,size(data)]);
for i=1:size(data):1
avrgdata(1,i)= (sum(n(i:i+windsz-1)))/windsz;
end
Oluremi Falowo
Oluremi Falowo 2019 年 10 月 25 日
stil not getting an array
Oluremi Falowo
Oluremi Falowo 2019 年 10 月 25 日
i have been asked to not use an preexisting fucntions to do it
Guillaume
Guillaume 2019 年 10 月 25 日
編集済み: Guillaume 2019 年 10 月 25 日
You have not answered my first question. Your for still doesn't make any sense.
Also, please use the button to format your code in your posts.
I'm not convinced that your zeros([1,size(data)]) makes sense either. It's going to create a 3D matrix.
Oluremi Falowo
Oluremi Falowo 2019 年 10 月 25 日
I have changed the orginal code now (apologies, i am still very new to this) . for my current class on convolution i have been asked to code the moving average equation so using the movmean function would be cheating.
Guillaume
Guillaume 2019 年 10 月 25 日
If it's a class on convolution, aren't you supposed to use a convolution to calculate the moving average then?

サインインしてコメントする。

回答 (1 件)

KALYAN ACHARJYA
KALYAN ACHARJYA 2019 年 10 月 25 日
編集済み: KALYAN ACHARJYA 2019 年 10 月 25 日

0 投票

i have been asked to not use an preexisting fucntions to do it
Here I have used some basic functions only, may be allow, the following exampe better illustrates you regarding the average sum of segment of array data. Please note that the length of the data and window size is perfectly divisible, other wise we have do certains considerations or handling the remains extra elements
data=rand(1,30); % Just for example
wind_size=3; % If w size is 3, it means the result arrat will be length of length(data)/3
avrgdata=zeros(1,length(data)/3);
for i=1:length(avrgdata)
avrgdata(i)=sum(data(i:i+2))/wind_size;
end
avrgdata

2 件のコメント

Oluremi Falowo
Oluremi Falowo 2019 年 10 月 25 日
I don't think we have the same idea of a moving poin average. In my case the input and output should be the same length.
KALYAN ACHARJYA
KALYAN ACHARJYA 2019 年 10 月 25 日
編集済み: KALYAN ACHARJYA 2019 年 10 月 25 日
Yes, multiple ways, averaging can done in arrays? Then is this?
data=rand(1,30); % Just for example
wind_size=3; % If w size is 3, it means the result arrat will be length of length(data)/3
avrgdata=zeros(1,length(data));
for i=1:length(avrgdata)-2
avrgdata(i)=sum(data(i:i+2))/wind_size;
end
avrgdata
How you going to handle boundary elements? Is zero padding or??
For example
data=[2 3 4 5 6 7]
What would be answer as per your calculation?

サインインしてコメントする。

カテゴリ

質問済み:

2019 年 10 月 25 日

編集済み:

2019 年 10 月 25 日

Community Treasure Hunt

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

Start Hunting!

Translated by