Remove a set of intervals from an array

23 views (last 30 days)
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!

Accepted Answer

Christopher McCausland
Christopher McCausland on 1 Jun 2022
Hi Ciara,
There are a couple of diffrent ways to do this, as always it depends on speed vs. understanding. A very understandable way would be to take the end elements, remove the ;argest ones and rinse and repeat till the start;
I should point out that I start with the end elements as I can remove them without changing the order of the previous elements, the same cannot be said if you start with the lower elements.
for k = length(startIntervals):-1:1 % for the three elements, start with the biggest and work back
data(startIntervals(k):endIntervals(k)) = []; % for the indexs remove the values with = []
end
The downside to this approach is that it can take longer the more elements need removed, so direct indexing can be faster. Let me know if this helps.
Kind regards,
Chrisopher

More Answers (2)

Jon
Jon on 1 Jun 2022
Edited: Jon on 1 Jun 2022
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

dpb
dpb on 1 Jun 2022
Edited: dpb on 1 Jun 2022
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 Comments
dpb
dpb on 1 Jun 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...

Sign in to comment.

Tags

Community Treasure Hunt

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

Start Hunting!