Sum five elements before a specific number

조회 수: 7 (최근 30일)
Karoline Qasem
Karoline Qasem 2017년 7월 17일
댓글: Karoline Qasem 2017년 7월 18일
Hi, I used "findpeaks" to find the IDs of all the peaks in my data. I need for each peak to find the average of five elements before it. the problem is that some of the peaks don't have five elements before it (say the second value is a peak, therefore, there is only one value before it). How can I tell matlab to average whatever is available but not more than 5 elements?
  댓글 수: 4
Image Analyst
Image Analyst 2017년 7월 18일
편집: Image Analyst 2017년 7월 18일
Please attach your data. I'm puzzled as to why your indexes are not in ascending order. I need to see the data to learn why. When I run it, ID always has increasing index values. In the meantime, check out movmean() to get a filtered vector, then pick out the values of the filtered array at those indexes.
Karoline Qasem
Karoline Qasem 2017년 7월 18일
편집: Walter Roberson 2017년 7월 18일
The data is very huge and messy. The reason why it is going decending is because I used this:
NP=6;
[pks,ID]= findpeaks(Q,'NPeaks',NP,'SortStr','descend');

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

채택된 답변

Walter Roberson
Walter Roberson 2017년 7월 18일
data_before = Yourdata(max(1, idx-5) : idx-1)
Note that in the case that idx is 1, this will give the empty result, as "empty" is all of the data that is available before the location.
  댓글 수: 4
Walter Roberson
Walter Roberson 2017년 7월 18일
편집: Walter Roberson 2017년 7월 18일
T = [nan(5,1); X(:)];
lenT = length(T);
idx = sub2ind([lenT, 1], 5+bsxfun(@plus, ID, -5:-1), ones(length(ID), 5));
result = nanmean( T(idx), 2 );
Karoline Qasem
Karoline Qasem 2017년 7월 18일
Amazing! Thanks a lot!

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Multirate Signal Processing에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by