根据索引向量切片数据向量

时间:2018-12-02 14:57:31

标签: matlab matrix vectorization slice matrix-indexing

我将时间序列数据存储在一个长向量中,我想在每个刺激触发器周围从中切出片段。我知道如何使用for循环解决此问题,但我想知道如何使用矢量化语法来解决这个问题。

考虑以下最小工作示例:

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);

0 个答案:

没有答案
相关问题