# Remove a set of intervals from an array

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

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

### 채택된 답변

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 = []
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

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

### 추가 답변 (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)) = [];
end
##### 댓글 수: 0이전 댓글 -2개 표시이전 댓글 -2개 숨기기

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

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
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);
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없음 표시없음 숨기기
Jon 2022년 6월 1일
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 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에서 Logical에 대해 자세히 알아보기

### Community Treasure Hunt

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

Start Hunting!

Translated by