![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/462095/image.png)
How to set a moving window
조회 수: 65 (최근 30일)
이전 댓글 표시
Hi everyone. I need help with a moving window creation: I'm analyzing behavioral data collected from rats and I need to create a moving window showing how their psycometric curves change through each session. I don't need help with the psycometric curves creation, but I really don't know how to set the loop to create a moving window with a window of 50 and a step of 1...
I need to do this with a LOOP, so I don't want to use any function (like movmean or conv for example)
I really hope you can help me because this is driving me crazy
댓글 수: 0
채택된 답변
Image Analyst
2020년 12월 15일
What do you want to do for elements 1-49? Or the final 49 elements? Do you want the window to shrink as your window would leave the array? Do you just want those places where the window fits complete in your array? Did you just try a simple for loop:
ratData = rand(1, 500); % Sample data
windowWidth = 50;
averagedData = zeros(1, length(ratData) - windowWidth);
for k = 1 : length(ratData) - windowWidth
averagedData(k) = mean(ratData(k:k+windowWidth-1));
end
plot(ratData, 'b-', 'LineWidth', 2);
hold on;
plot(averagedData, 'r-', 'LineWidth', 2);
grid on;
legend('Actual', 'Averaged');
msgbox('Done!');
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/462095/image.png)
댓글 수: 3
Image Analyst
2020년 12월 17일
Maybe he doesn't know about the movmean() function so that's the reason why he suggested a loop. I see no real NEED for it to be a loop. Suggest the built-in movmean function to him. Or just go ahead and use it. I doubt he'd really care HOW you got it done.
Anyway, didn't my code do a manual 50 element moving mean? Not sure why it didn't meet your needs.
추가 답변 (1개)
Steven Lord
2020년 12월 15일
Use the value of the for loop variable as the index of the first element in the window. Add a quantity to get the index of the last element in the window. Use linear indexing to extract the elements in the window.
x = (1:10).^2
y = x(4:7) % [16 25 36 49]
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Data Preprocessing에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!