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.

 채택된 답변

Image Analyst
Image Analyst 2016년 10월 25일

0 개 추천

Try something like
% Find all time differences:
timeDiffs = abs(timeStamps - spikeTimes);
% Find out which has the smallest difference:
[minTimeDiff, indexOfMin] = min(timeDiffs);
% Get the value from column 1.
result = dataColumn1(indexOfMin)

댓글 수: 5

Krispy Scripts
Krispy Scripts 2016년 10월 25일
timeStamps and spikeTimes are different sizes so I am not sure that it is possible.
Image Analyst
Image Analyst 2016년 10월 25일
편집: Image Analyst 2016년 10월 26일
Oh, they're not synced up. Then you should use pdist2 like Roger suggested, if you have the Statistics and Machine Learning Toolbox. Look for the smallest non-zero value.
Krispy Scripts
Krispy Scripts 2016년 10월 25일
I am confused on how the row, column outputs can be used to find the those corresponding data points?
Krispy Scripts
Krispy Scripts 2016년 10월 25일
Would this be simpler if instead of getting the closest, I got the one greater than or equal to?
Sorry - that's what I get for tossing something off the top of my head and not actually testing it. Ignore that code (I'll delete it) and use this instead:
numPoints1 = 6;
timeStamps = rand(numPoints1, 1) % Sample data.
numPoints2 = 3;
spikeTimes = rand(numPoints2, 1) % Sample data.
t1 = [timeStamps, zeros(length(timeStamps), 1)];
t2 = [spikeTimes, zeros(length(spikeTimes), 1)];
distances = pdist2(t1, t2)
minDistance = min(distances(distances>0))
[row, column] = find(distances == minDistance)
fprintf('The minimum time difference is %f and goes between\n timeStamp(%d) = %f, and\n spikeTimes(%d) = %f\n',...
minDistance, row, timeStamps(row), column, spikeTimes(column));

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Roger Stafford
Roger Stafford 2016년 10월 25일

1 개 추천

I recommend you use the ‘pdist2’ function using the “Smallest” option. It is described at:
https://www.mathworks.com/help/stats/pdist2.html

카테고리

도움말 센터File Exchange에서 Statistics and Machine Learning Toolbox에 대해 자세히 알아보기

질문:

2016년 10월 25일

댓글:

2016년 10월 26일

Community Treasure Hunt

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

Start Hunting!

Translated by