Hi, Can anyone tell me how to find average of three successive values in matlab, i have an array of 200 values
이전 댓글 표시
I have 200 values and need to find average of three successive values in complete array
답변 (2개)
Azzi Abdelmalek
2013년 2월 9일
편집: Azzi Abdelmalek
2013년 2월 9일
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
Image Analyst
2013년 2월 9일
편집: Image Analyst
2013년 2월 9일
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.
카테고리
도움말 센터 및 File Exchange에서 Elementary Math에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!