필터 지우기
필터 지우기

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

조회 수: 1 (최근 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년 3월 16일
That was a typo, it should have been ge (and le instead of lte).
Now fixed.
Krispy Scripts
Krispy Scripts 2016년 3월 17일
I am getting an empty matrix for behavior times?

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

채택된 답변

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에서 Shifting and Sorting Matrices에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by