Find the average in a window of random variables

2 ビュー (過去 30 日間)
Tino
Tino 2020 年 5 月 28 日
編集済み: Adam Danz 2020 年 8 月 22 日
Hi pls
I have 10 random variables ( 2,4,5,6,3,4,5,7,8,6)
I want to write a matlab code that will do this computation using 5 window length ( k = 5)
1st window length (2,4,5,6,3)
2nd window length ( 4,5,7,8,6)
then I can find the average of each windom numbers
1st window average = 4
2nd window average = 6
Thanks in advance
Thanks in advance
  3 件のコメント
Tino
Tino 2020 年 5 月 28 日
If I can then I wouldnt be asking for help
darova
darova 2020 年 5 月 28 日
70 questions asked. Hello

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

採用された回答

Adam Danz
Adam Danz 2020 年 5 月 28 日
編集済み: Adam Danz 2020 年 8 月 22 日
Matlab has movmean() but it I don't think it can move by groups of n elements.
This demo below computes a moving average for values [1:n, 1*n+1:2*n, 2*n+1:3*n, 3*n+1:4*n, etc...]
If the number of data points is not dividible by n, the data are padded with NaN values and the last average will only consider non-nan values.
data = 1:22; % Demo data
winSz = 5; % Window size
% Reshape data into matrix.
% NOTE: 'data' must contain a number of element divisibly by 'winSz'.
% Otherwise, 'data' will be padded with NaN values so that it is
% divisible by 'winSz'.
if rem(numel(data),winSz)>0
nanAppend = nan(winSz - rem(numel(data),winSz),1);
else
nanAppend = [];
end
dataMat = reshape([data(:); nanAppend], winSz, []);
movingAverage = mean(dataMat,1,'Omitnan');
% Result
% movingAverage =[3 8 13 18 21.5]
To compute the means of groups that share endpoints (e.g. indices 1:5, 5:9, 9:13, ...), see this answer.

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

タグ

製品


リリース

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by