I have a vector of spike times (action potentials from a neuron) and a vector of stimulus event timestamps. I want to create a PSTH to see if the stimulus influences the spike rate of the neuron. I can do this by looping through each stimulus event (see simple example below), but this is very slow for long experiments where there are over 30,000 stimulus events and many neurons are being recorded.
How can this be done without for loops?
Example of the slow way:
% set variables
spikeTimes = [0.9 1.1 1.2 2.5 2.8 3.1];
stimTimes = [1 2 3 4 5];
preStimTime = 0.2;
postStimTime = 0.3;
for iStim = 1:length(stimTimes)
% find spikes within time window
inds = find((spikeTimes > (stimTimes(iStim) - preStimTime)) & (spikeTimes < (stimTimes(iStim) + postStimTime)));
% align spike times to stimulus onset
stimONtimes = spikeTimes(inds) - stimTimes(iStim);
% store times in array for plotting
PSTH_array(iStim,1:length(stimONtimes)) = stimONtimes;
end