I have time series data stored in a long vector and I want to cut out snippets from it around each stimulus trigger. I know how to solve this with a for-loop, but I was wondering how to do it in a vectorized syntax.
Consider the following minimal working example:
function q53581423
data = randi(255, 8000000, 1);
trigger_idx = sort(randsample(7998901, 10) + 99);
start_idx = trigger_idx-50; % start cut out 50 samples before the trigger
end_idx = trigger_idx+200; % end cut out 200 samples after the trigger
%% Sequential solution using a for-loop (working)
aligned_data = zeros(numel(trigger_idx), 251);
for indT = 1:numel(trigger_idx)
aligned_data(indT,:) = data(start_idx(indT):end_idx(indT)).';
end
%% Vectorized attempt (not working)
aligned_data = data(start_idx:end_idx);