Hi, Can anyone tell me how to find average of three successive values in matlab, i have an array of 200 values

Answers (2)

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

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.
@Image Analyst: Didn't know that. Thanks. I guess I should follow my own advice and read the documentation sometimes.

Sign in to comment.

Categories

Find more on Elementary Math in Help Center and File Exchange

Tags

No tags entered yet.

Asked:

on 9 Feb 2013

Community Treasure Hunt

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

Start Hunting!