Skip to main content
AI Assist is now on Stack Overflow. Start a chat to get instant answers from across the network. Sign up to save and share your chats.
Rollback to Revision 2
Link
Matt Krause
  • 1.2k
  • 14
  • 33

Vectorizing computing array indexing/subsetting window in Matlab

edited tags; edited title
Link
smci
  • 34.2k
  • 21
  • 118
  • 152

Vectorizing computing array indexing/subsetting window in Matlab

added 24 characters in body
Source Link
Matt Krause
  • 1.2k
  • 14
  • 33

Suppose I have a long data vector y, plus some indices into it. I want to extract a short snippet or window around every index.

For example, suppose I want to construct a matrix containing 64 samples before and 64 samples after every value that is below three. This is trivial to do in a for-loop:

WIN_SIZE = 64; % Sample data with padding data = [nan(WIN_SIZE,1); randn(1e6,1); nan(WIN_SIZE,1)];   % Sample events, could be anything index = find(data < 3); % Sample events, could be anything snippets = nan(length(index), 2*WIN_SIZE + 1); for ii=1:length(index) snippets(ii,:) = data((index(ii)-64WIN_SIZE):(index(ii)+64+WIN_SIZE)); end 

However,this is not blazingly fast. Is there any way to vectorize (or otherwise speed up) this operation?

(In case this is unclear, the index could be anything and may not necessarily be a property of the data; I just wanted something simple to illustrate the idea.)

Suppose I have a long data vector y, plus some indices into it. I want to extract a short snippet or window around every index.

For example, suppose I want to construct a matrix containing 64 samples before and 64 samples after every value that is below three. This is trivial to do in a for-loop:

WIN_SIZE = 64; % Sample data with padding data = [nan(WIN_SIZE,1); randn(1e6,1); nan(WIN_SIZE,1)]; index = find(data < 3); % Sample events, could be anything snippets = nan(length(index), 2*WIN_SIZE + 1); for ii=1:length(index) snippets(ii,:) = data((index(ii)-64):(index(ii)+64)); end 

However,this is not blazingly fast. Is there any way to vectorize (or otherwise speed up) this operation?

(In case this is unclear, the index could be anything and may not necessarily be a property of the data; I just wanted something simple to illustrate the idea.)

Suppose I have a long data vector y, plus some indices into it. I want to extract a short snippet or window around every index.

For example, suppose I want to construct a matrix containing 64 samples before and 64 samples after every value that is below three. This is trivial to do in a for-loop:

WIN_SIZE = 64; % Sample data with padding data = [nan(WIN_SIZE,1); randn(1e6,1); nan(WIN_SIZE,1)];   % Sample events, could be anything index = find(data < 3); snippets = nan(length(index), 2*WIN_SIZE + 1); for ii=1:length(index) snippets(ii,:) = data((index(ii)-WIN_SIZE):(index(ii)+WIN_SIZE)); end 

However,this is not blazingly fast. Is there any way to vectorize (or otherwise speed up) this operation?

(In case this is unclear, the index could be anything and may not necessarily be a property of the data; I just wanted something simple to illustrate the idea.)

Source Link
Matt Krause
  • 1.2k
  • 14
  • 33
Loading