Azzera filtri
Azzera filtri

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 visualizzazione (ultimi 30 giorni)
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?

Risposta accettata

dpb
dpb il 26 Ott 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--
  1. Use the 'last' optional argument and search for '<=' instead of '>=' which will give you the last value that isn't over, or
  2. 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.
for k=1:length(spiketimesmatrix)
dataout(k)=datamatrix(find(min(abs(spiketimesmatrix(k)-datamatrix(:,2))),1));
end
  4 Commenti
dpb
dpb il 1 Nov 2016
Sure, why not? Simply use a LHS variable; good practice will preallocate based on size(spike) array by number of columns needed.

Accedi per commentare.

Più risposte (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by