Mean value of a subarray

조회 수: 5 (최근 30일)
EldaEbrithil
EldaEbrithil 2020년 8월 22일
댓글: EldaEbrithil 2020년 8월 22일
Hi all
i have an array V=1x115 i want to create another array which contains the mean value neatly of the first 8 V values than of the subsequent 8 and so on... in pratical terms: mean(V(1:8)), mean(V(8:16)),...mean(V(112:end))
Thank you for the help
Regards
  댓글 수: 2
Adam Danz
Adam Danz 2020년 8월 22일
편집: Adam Danz 2020년 8월 22일
1:8 contains 8 values.
8:16 contains 9 values.
If you meant to write 1:8 and 8:15 (each containing 8 values), see my answer.
If you meant to write 1:8, 9:16 (each containing 8-values), see the link at the top of my answer.
If your window sizes vary, you'll need to explain your problem with more detail.
EldaEbrithil
EldaEbrithil 2020년 8월 22일
Thank you Adam
I solved the problem thanks to your upper link, now i understand the logic for the moving average
Regards

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

채택된 답변

Adam Danz
Adam Danz 2020년 8월 22일
편집: Adam Danz 2020년 8월 22일
The answer in this link computes segmented averages as you describe except instead of averaging 1:8, 8:15, ... , it averages 1:8, 9:16, ..., .
Solution
To replicate the edges, you just have to add three line.
The full solutions is shown in this demo.
data = 1:22; % Demo data
winSz = 5; % Window size; winSz=5 results in indices of (1:5, 5:9, 9:13, ...)
replications = repmat([2;ones(winSz-2,1)],ceil(numel(data)/(winSz-1)),1);
dataPrep = repelem(data(:),replications(1:numel(data)));
dataPrep(1) = []; % do not duplicate 1st datapoint
% Reshape dataPrep into matrix.
% NOTE: 'dataPrep' must contain a number of element divisibly by 'winSz'.
% Otherwise, 'dataPrep' will be padded with NaN values so that it is
% divisible by 'winSz'.
if rem(numel(dataPrep),winSz)>0
nanAppend = nan(winSz - rem(numel(dataPrep),winSz),1);
else
nanAppend = [];
end
dataMat = reshape([dataPrep; nanAppend], winSz, []);
movingAverage = mean(dataMat,1,'Omitnan');
Results
The original data:
data =
Columns 1 through 20
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
Columns 21 through 22
21 22
The blocked version where means are computed over each column
dataMat =
1 5 9 13 17 21
2 6 10 14 18 22
3 7 11 15 19 NaN
4 8 12 16 20 NaN
5 9 13 17 21 NaN
The means (within a 5-element window)
movingAverage =
3 7 11 15 19 21.5
  댓글 수: 3
Adam Danz
Adam Danz 2020년 8월 22일
편집: Adam Danz 2020년 8월 22일
Socially distant high five!
EldaEbrithil
EldaEbrithil 2020년 8월 22일
Ahahah xD

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

추가 답변 (0개)

카테고리

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

태그

제품


릴리스

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by