- Use the 'last' optional argument and search for '<=' instead of '>=' which will give you the last value that isn't over, or
- Use the search for a minimum difference instead, which actually is what your topic says you're looking for, anyway. Note the search for the first/last over/under a given value isn't necessarily the closest, only the one before the sign change of the difference.
Attempting to find data points of two different matrices based off of time, but outcome is that I am getting the same value repeatedly
1 visualización (últimos 30 días)
Mostrar comentarios más antiguos
Krispy Scripts
el 26 de Oct. de 2016
Comentada: dpb
el 1 de Nov. de 2016
I have one matrix with data in the first column and time stamps in the second column (datamatrix.mat). The next matrix contains spiketimes (spiketimematrix.mat). I want to get the data point in the first column of first matrix that is the closest time point corresponding to the spike times in spiketimematrix.mat. For example, the first spiketime is 166.1670, which corresponds to the closet time point of 166.1696 and corresponds with the data point 2.5281. I have tried using the following code:
for k=1:length(spiketimesmatrix)
dataout(k)=datamatrix(find(spiketimesmatrix(k)>=datamatrix(:,2),1));
end
However, I am getting the first value of =1.8577 repeatedly and I am not sure why?
0 comentarios
Respuesta aceptada
dpb
el 26 de Oct. de 2016
'Cuz you're not restricting the search to only begin with the last found location and by default find will find the first location that satisfies the condition. If the data are ordered in ascending order, then once you have a location you're looking for that is greater, then any other that is that larger or greater will also satisfy...
Two ways to fix--
for k=1:length(spiketimesmatrix)
dataout(k)=datamatrix(find(min(abs(spiketimesmatrix(k)-datamatrix(:,2))),1));
end
4 comentarios
dpb
el 1 de Nov. de 2016
Sure, why not? Simply use a LHS variable; good practice will preallocate based on size(spike) array by number of columns needed.
Más respuestas (0)
Ver también
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!