How do I calculate the RMS of every 5 values?
조회 수: 26 (최근 30일)
이전 댓글 표시
Hello, I have a vector of 66000 values and I would like to know the root mean square (RMS) of every 5 values. Do I need a loop for this? Or is there some easier way to solve this?
Thanks in advance! Shawn
댓글 수: 0
채택된 답변
Guillaume
2016년 11월 3일
편집: Guillaume
2016년 11월 3일
If you mean you want the RMS of the first 5 values, followed by the RMS of the next 5 values, etc., then assuming your vector length is a multiple of 5 simply reshape it into columns of 5 elements:
rms = sqrt(mean(reshape(yourvector, 5, []) .^ 2));
If you mean you want a moving rms (rms of elements 1-5, rms of elements 2-6, etc.), then use movmean:
movrms = sqrt(movmean(yourvector .^ 2, 5));
댓글 수: 5
Julien Rodriguez
2022년 1월 7일
Really Guillaume I don't know how to thank you properly.
You should be in the Avengers cause you're my little hero.
plot(love);
krishna teja
2023년 7월 28일
편집: krishna teja
2023년 7월 28일
awesome piece of code, Guillaume !!! have a great year
reduced my code execution from 1 sec to 0.025 sec !!
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!