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
2016 年 3 月 16 日
That was a typo, it should have been ge (and le instead of lte).
Now fixed.
採用された回答
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
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 Center および File Exchange で Characters and Strings についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!