MATLAB: Divide a dataset into subsets

indexingsignal processing

Hello everyone
I have a dataset named chan56 which is 1619936×1 and a vector called markers which is 1×202. The vector markers has the events of the dataset chan56 and i want to split chan56 into subets that contain -1200 indexes from each event until +300. I tried
Data = []
DataMatrix =[zeros(1,length(markers)*1500)];
for n =1 :length(chanPOz)
for i =1:length(markers)
if markers(i) == chanPOz(n)
T = chanPOz(n);
Data = chanPOz(n-1200:n+300);
DataMatrix = [DataMatrix DataMatrix+Data];
end
end
end
But i cant take the DataMatrix

Best Answer

This is what you're after, I think. No need for a loop over each point in chanPOz
N = numel(markers) ;
% pre-allocate the result:
newData = zeros(N, 1501) ; % 1 extra !! [x-1200:x+300] are 1501 values
% loop over each marker
for n = 1:N
idx = markers(n) ; % the n-th index into chanPOz
newData(n,:) = chanPOz(idx-1200:idx+300); % get the data and store this in the n-th row
end