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

回答 (2 件)

Azzi Abdelmalek
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,[]))
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 件のコメント

Image Analyst
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.
José-Luis
José-Luis 2013 年 2 月 9 日
編集済み: José-Luis 2013 年 2 月 9 日
@Image Analyst: Didn't know that. Thanks. I guess I should follow my own advice and read the documentation sometimes.

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

カテゴリ

ヘルプ センター および File ExchangeScope Variables and Generate Names についてさらに検索

タグ

タグが未入力です。

質問済み:

2013 年 2 月 9 日

Community Treasure Hunt

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

Start Hunting!

Translated by