Clear Filters
Clear Filters

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

1 view (last 30 days)
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 Comments
Guillaume
Guillaume on 16 Mar 2016
That was a typo, it should have been ge (and le instead of lte).
Now fixed.

Sign in to comment.

Accepted Answer

Guillaume
Guillaume on 26 Feb 2016
Edited: Guillaume on 17 Mar 2016
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 Comment
Guillaume
Guillaume on 17 Mar 2016
_"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

Sign in to comment.

More Answers (0)

Categories

Find more on Shifting and Sorting Matrices in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!