calculate a vector from consecutive vectors without a loop

조회 수: 2 (최근 30일)
Guy Eyal
Guy Eyal 2013년 6월 12일
Hi
I would like to receive the mean value of each 10 consecutive values of a vector. Is it possible to run it without a for loop?
Using a for it would have been:
for ix=1:length(X)-9 mean_X = mean(X(ix:ix+9)) end
Thanks Guy

채택된 답변

Kye Taylor
Kye Taylor 2013년 6월 12일
편집: Kye Taylor 2013년 6월 12일
Totally.. try something like
yourVector = 1:100;
avgWindow = 1/10*ones(1,10); % each value is 1/10
movingAverage = conv(yourVector,avgWindow,'valid')
Compare with
for i = 1:numel(yourVector)-9
movingAverageLoop(i) = mean(yourVector(i:i+9));
end

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Resizing and Reshaping Matrices에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by