Hi, Can anyone tell me how to find average of three successive values in matlab, i have an array of 200 values
Show older comments
I have 200 values and need to find average of three successive values in complete array
1 Comment
Image Analyst
on 9 Feb 2013
Which three?
Answers (2)
Azzi Abdelmalek
on 9 Feb 2013
Edited: Azzi Abdelmalek
on 9 Feb 2013
x=rand(201,1);
m=mod(numel(x),3)
if m>0
x(end+1:end+3-m)=mean(x(end-m+1:end))
end
out=mean(reshape(x,3,[]))
your_vals = randi(100,1,200);
filterLength = 3;
%One option
your_result_1 = conv(your_vals,ones(1,filterLength)./filterLength);
your_result_1 = your_result_1(filterLength:end-filterLength+1); %Getting rid of the tails
%Alternatively
your_result_2 = filter(ones(1,filterLength)./filterLength,1,your_vals);
your_result_2(1:filterLength-1) = []; %Getting rid of the tails
%One more still
your_result_3 = arrayfun(@(x) mean( your_vals(x:x+filterLength-1) ), 1:numel(your_vals) - filterLength + 1); %one liner
%Rounding up
your_result_4 = mean(cell2mat(arrayfun(@(x) circshift(your_vals,[0 -x]),(0:filterLength-1)','uniformoutput',false)));
your_result_4 = your_result_4(1:end-filterLength+1); %Getting rid of the tails
Plus many others
2 Comments
Image Analyst
on 9 Feb 2013
Edited: Image Analyst
on 9 Feb 2013
I hate it when people don't put enough information into their question so it causes us to have to guess at multiple possible meanings and come up with multiple possible solutions. By the way, with your conv() solution, there are boundary effect options of 'same' and 'valid' to handle "getting rid of tails" internally.
Categories
Find more on Elementary Math 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!