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

답변 (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,[]))
José-Luis
José-Luis 2013년 2월 9일
편집: José-Luis 2013년 2월 9일
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

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.

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Elementary Math에 대해 자세히 알아보기

태그

아직 태그를 입력하지 않았습니다.

질문:

2013년 2월 9일

Community Treasure Hunt

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

Start Hunting!

Translated by