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 次查看(过去 30 天)
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?

采纳的回答

dpb
dpb 2016-10-26
'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 个评论
dpb
dpb 2016-11-1
Sure, why not? Simply use a LHS variable; good practice will preallocate based on size(spike) array by number of columns needed.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 String Parsing 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by