Create a sliding window

조회 수: 133 (최근 30일)
Miguel Albuquerque
Miguel Albuquerque 2022년 7월 2일
댓글: Matt J 2022년 7월 2일
Hey guys, thanks in advance
I have this code, that gives me a vector, samples , that has 10 000 000 values( a lot). I wanted to divide this vector in 500 000 samples do all the processing code I have , and choose the next 500 000 samples, with a sliding window, till i finished all the samples.
So first iteration I wanted samples(1:500 000), 2nd iteration(501000: 1000000), etc...
But with a sliding window function, can I do that
Thank you

채택된 답변

Image Analyst
Image Analyst 2022년 7월 2일
Ten million samples is not a lot, but anyway a simple intuitive way is
for k = 1 : 500000 : length(vec) % Should be 20 iterations if vec is 10 million long.
thisWindow = vec(k : k + 499999);
% Now do something with thisWindow.
end

추가 답변 (1개)

Matt J
Matt J 2022년 7월 2일
편집: Matt J 2022년 7월 2일
matrix=reshape(vector,5e5,[])
This gives a matrix whose i-th window is matrix(:,i). How best to process that, though, depends on the specifics of the sliding window operation.
  댓글 수: 2
Miguel Albuquerque
Miguel Albuquerque 2022년 7월 2일
With that Im not getting a sliding window, I needed to change the values in the matrix, is that faster of producing a for cycle instead, for example?
Matt J
Matt J 2022년 7월 2일
It can be faster. For example, if you are calculating the mean over each window,
mean( reshape(vector,5e5,[]) )
will be faster than looping.

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

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

제품


릴리스

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by