How to create a sliding window function over column vector M

11 ビュー (過去 30 日間)
Thashen  Naidoo
Thashen Naidoo 2019 年 11 月 24 日
編集済み: Jeff 2024 年 2 月 8 日
If vector M is a column of 30000 points of temperature, How do I create a sliding window function that goes from 1 to 500, 2 to 501, 3 to 502 ...till the end. So the window length is 500.

採用された回答

Thiago Henrique Gomes Lobato
Thiago Henrique Gomes Lobato 2019 年 11 月 24 日
You can easily implement a loop for this
A = [1,2,3,4,5,6]'; % Easy vector to verify output
fun = @(x)mean(x); % Your function
WindowLength = 2;
Output = zeros(length(A)-WindowLength,1);
for idx = 1:length(A)-WindowLength
Block = A(idx:idx+WindowLength);
Output(idx) = fun(Block);
end
Output
Output =
2
3
4
5
Or, if you want to have it in many places, just create a function for it:
function output = SlideFun(x,WindowLength,fun)
Output = zeros(length(x)-WindowLength,1);
for idx = 1:length(x)-WindowLength
Block = x(idx:idx+WindowLength);
Output(idx) = fun(Block);
end
end
  1 件のコメント
Jeff
Jeff 2024 年 2 月 8 日
編集済み: Jeff 2024 年 2 月 8 日
Since I found this and ran into this issue, maybe this will help someone else.
Output = zeros(length(x) + 1 - WindowLength,1);
for idx = 1:length(x) + 1 - WindowLength
Block = x(idx:idx + WindowLength - 1);
Output(idx) = fun(Block);
end

サインインしてコメントする。

その他の回答 (0 件)

Community Treasure Hunt

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

Start Hunting!

Translated by