Sorting data in one column based on the time stamps in the alternate column

In one matrix, one column has data and the column next to it has time stamps. I have another matrix where the first column is start times and the second column is stop times of the interval. I would like to get the data from the first matrix based on the intervals of the second matrix, where the intervals correspond to the time stamps in the first matrix second column. I would like to output the data into a matrix, where the interval data is in individual columns in that matrix.
Does this make sense? I have attached example matrices. So from the first column dataset.mat, I want the data set that corresponds to the interval.mat (3 intervals) and output them into three different columns corresponding to each interval.

 Respuesta aceptada

selected = arrayfun(@(ROW) dataset(dataset(:,2) >= intervals(ROW,1) & dataset(:,2) <= intervals(ROW,2), 1), 1:size(intervals,1), 'Uniform', 0);
if all(diff(cellfun(@length, selected)) == 0)
selected = horzcat( selected{:} );
else
fprintf('the intervals select different number of rows, had to leave selected as a cell array\n');
end

5 comentarios

Could you explain the fprintf here?
The out put is in a cell array. Is it possible to put this as a matrix?
I tested with the data you supplied and it does produce a matrix with that data. However with a different set of intervals it is possible that the intervals select different numbers of data points each, such as possibly 5001 for one and 4999 for another. In such a case you would be asking to have a numeric matrix in which the columns were different sizes, and that is something that MATLAB does not allow so it becomes necessary to keep them in a cell array because of the different sizes.
I would need to recheck the code for the case where there was only one interval
Ah, thank you so much. My code was splitting different sets of intervals into intervals and adding extra intervals of 0 that were causing the problem you described above.
I would need to recheck the code for the case where there was only one interval
Okay I checked and the code will work for that case.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Preguntada:

el 17 de Oct. de 2016

Comentada:

el 18 de Oct. de 2016

Community Treasure Hunt

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

Start Hunting!

Translated by