Remove a set of intervals from an array

조회 수: 32 (최근 30일)
Ciara Gibbs
Ciara Gibbs . 2022년 6월 1일
편집: dpb . 2022년 6월 1일
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
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!

채택된 답변

Christopher McCausland
Christopher McCausland 2022년 6월 1일
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 = []
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,

추가 답변 (2개)

Jon 2022년 6월 1일
편집: Jon 님. 2022년 6월 1일
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)) = [];

dpb 2022년 6월 1일
편집: dpb 님. 2022년 6월 1일
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
or, student exercise, write the above with arrayfun() to avoid explicit loop.
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
dpb 2022년 6월 1일
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...

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


Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기


Community Treasure Hunt

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

Start Hunting!

Translated by