フィルターのクリア

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 件のコメント
Krispy Scripts
Krispy Scripts 2016 年 11 月 1 日
Great! Can I output this into a matrix instead of displaying it?
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 Center および File ExchangeString Parsing についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by