Borrar filtros
Borrar filtros

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

1 visualización (últimos 30 días)
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 comentarios
Guillaume
Guillaume el 16 de Mzo. de 2016
That was a typo, it should have been ge (and le instead of lte).
Now fixed.
Krispy Scripts
Krispy Scripts el 17 de Mzo. de 2016
I am getting an empty matrix for behavior times?

Iniciar sesión para comentar.

Respuesta aceptada

Guillaume
Guillaume el 26 de Feb. de 2016
Editada: Guillaume el 17 de Mzo. de 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 comentario
Guillaume
Guillaume el 17 de Mzo. de 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

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Shifting and Sorting Matrices en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by