Sorting a matrix of data using another matrix of start and stop times

2 次查看(过去 30 天)
I have a data set of values in column with time stamps at regular intervals in the next column. I have another set of start and stop times in consecutive columns. I tried writing a function to get data in between start and stop times for it (at the bottom), but I am getting errors like input matrix must agree, inputs must have the same size etc. I have tried writing it multiple ways to get around this, but I can't. The data set is very large and there are only about ten start and stop times that I would like to get data for so it is of course not going to be equal matrices.
Any ideas?
function [behaviortimes] = datafilter(spiketimes,frequencydata)
%DATAFILTER Function returns filtered data times for frequencies given
%start and stop times of behaviors according to the frequencies
% Start and stop times should be in two columns of a matrix
% frequency data
if (frequencydata(:,2)>0) & (spiketimes(:,1)>0) & (spiketimes(:,2)>0);
behaviortimes=frequencydata(frequencydata(:,2)>=(spiketimes(:,1) & frequencydata(:,2)<=(spiketimes(:,2))));
end
end
  5 个评论

请先登录,再进行评论。

采纳的回答

Guillaume
Guillaume 2016-2-26
编辑:Guillaume 2016-3-17
The following will tell you which row of frequencydata is in which interval of spiketimes:
ininterval = bsxfun(@ge, frequencydata(:, 2), spiketimes(:, 1).') & bsxfun(@le, frequencydata(:, 2), spiketimes(:, 2).')
Each row of ininterval correspond to the same row of frequencydata. Each column correspond to a row of spiketimes. A 1 indicates that the row is within the interval. To reduce it to a column vector:
isinterval = any(ininterval, 2)
And to get only the rows of frequencydata for which isinterval is true:
behaviortimes = frequencydata(isinterval, :)
As a one liner:
behaviortimes = frequencydata(any(bsxfun(@ge, frequencydata(:, 2), spiketimes(:, 1).') & bsxfun(@le, frequencydata(:, 2), spiketimes(:, 2).'), 2), :);
  1 个评论
Guillaume
Guillaume 2016-3-17
_"I am getting an empty matrix for behavior times?" I made another typo in the one liner, it should have been any instead of all. The detailed code was correct.
frequencydata = repmat(1:100, 2, 1)'; %demo data, data (col 1)and timestamp (col2) have same value for easy diagnostic
spiketimes = [5 10; 35 37; 78 79];
behaviortimes = frequencydata(any(bsxfun(@ge, frequencydata(:, 2), spiketimes(:, 1).') & bsxfun(@le, frequencydata(:, 2), spiketimes(:, 2).'), 2), :)
behaviortimes =
5 5
6 6
7 7
8 8
9 9
10 10
35 35
36 36
37 37
78 78
79 79

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Characters and Strings 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by