Remove a set of intervals from an array
Mostrar comentarios más antiguos
Hi!
I have an array of time series data 1x1000 in size, I also have a set of start times and end times defining intervals which I need to remove from this time series
e.g.
data = [1:1000]
startIntervals = [1,60,400]
endIntervals = [5, 77, 606]
(so the intervals I need to remove are the points in data from 1-5, 60-77, 400-606)
Is there a straightforward way to do this?
Any help would be greatly appreciated. Thank you!
Respuesta aceptada
Más respuestas (2)
There may be a clever way of doing this without loops, but othewise you can remove the columns in your variable called data as follows
data = [1:1000]
startIntervals = [1,60,400]
endIntervals = [5, 77, 606]
% remove rows
for k = 1:numel(startIntervals)
data(data>=startIntervals(k)&data<=endIntervals(k)) = [];
end
That's the brute force way to code it and use MATLAB, but yes, it's just array indexing
% delete the rows identified -- looping construct
for i=numel(startIntervals):-1:1
data(startIntervals(i):endIntervals(i))=[];
end
or, student exercise, write the above with arrayfun() to avoid explicit loop.
Alternatively,
ikeep=~ismember(1:numel(data),cell2mat(arrayfun(@colon,i1,i2,'UniformOutput',false)));
data=data(ikeep);
ADDENDUM:
I started off above with the comment about brute force methods and then didn't follow up on the thought.
I'd ask how were the above intervals determined -- if there's any sort of rational reason for the sections based on the data itself or a know external factor, then the better approach would be to code that decision process instead -- then you don't have to have a set of magic numbers to use.
2 comentarios
Jon
el 1 de Jun. de 2022
I don't think that the above approach works.
First the indexing changes as you delete points.
Also you don't provide any index on startIntervals and endIntervals inside of the loop. startIntervals:endIntervals always will evaluate to [1 2 3 4 5].
Finally the variable startintervals on the for loop is not defined (capitalization error)
dpb
el 1 de Jun. de 2022
Good catches -- too quick and just at editor -- to do in the loop, one must always delete from the rear to the front -- iow, loop from N:-1:1 instead of 1:N.
Fixed both omissions above.
NB: the i1, i2 in the second arrayfun() expression are standins for the start,stop arrays above to shorten the line...
Categorías
Más información sobre Logical en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!