Finding mean values several times.

조회 수: 3 (최근 30일)
FV
FV 2020년 5월 7일
답변: Steven Lord 2020년 5월 7일
I want to find the mean of one part of a vector. I take the mean value of A1-5 , then A2-6, A3-7 and so on. I use this code: A(i)=mean(vector(i:i+4))
The problem is at the end of the vector. If its 10 A values I want the last average to go A5-10, A6-10, A7-10 and so on.
If it is a easy way to do this step several times I also want some help with this. So if 1=A1-5, 2=A2-6, 3=A3-7, I want to take average of 1-5, 2-6, 3-7 and so on.
I want to do this 4 or 5 times so I can find smothen out the values and find the peaks.
This was hard to describe, Hope you understand me.
  댓글 수: 2
David Wilson
David Wilson 2020년 5월 7일
One option is to pad the end with enough NaNs, then use mean with the 'omitnan' flag, mean(..., 'omitnan')
ChrizzzlP
ChrizzzlP 2020년 5월 7일
You may want to look into the 'smooth' function, it sounds like what you're trying to do is smoothing a function and this will do it for you, there are several methods

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

답변 (2개)

Paresh yeole
Paresh yeole 2020년 5월 7일
for i=1:n
if i+4<length(vector)
A(i) = mean(vector(i:i+4));
else
A(i) = mean(vector(i:end));
end
end
  댓글 수: 2
FV
FV 2020년 5월 7일
This works for my first problem. Can I repeat this step several times without copying the whole thing?
Paresh yeole
Paresh yeole 2020년 5월 7일
Yes. You can.

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


Steven Lord
Steven Lord 2020년 5월 7일
It sounds like the answer to the question you asked is movmean but you may not need to call movmean yourself.
Take a look at the smoothdata function to smooth your data and the islocalmax function to detect the local peaks. One of the options for how to smooth the data that smoothdata accepts is 'movmean'.

카테고리

Help CenterFile Exchange에서 Data Type Conversion에 대해 자세히 알아보기

태그

제품


릴리스

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by