# Remove a set of intervals from an array

26 visualizzazioni (ultimi 30 giorni)
Ciara Gibbs il 1 Giu 2022
Modificato: dpb il 1 Giu 2022
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!
##### 0 CommentiMostra -2 commenti meno recentiNascondi -2 commenti meno recenti

Accedi per commentare.

### Risposta accettata

Christopher McCausland il 1 Giu 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
##### 0 CommentiMostra -2 commenti meno recentiNascondi -2 commenti meno recenti

Accedi per commentare.

### Più risposte (2)

Jon il 1 Giu 2022
Modificato: Jon il 1 Giu 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
##### 0 CommentiMostra -2 commenti meno recentiNascondi -2 commenti meno recenti

Accedi per commentare.

dpb il 1 Giu 2022
Modificato: dpb il 1 Giu 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);
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 CommentiMostra NessunoNascondi Nessuno
Jon il 1 Giu 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 il 1 Giu 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...

Accedi per commentare.

### Categorie

Scopri di più su Logical in Help Center e File Exchange

### Community Treasure Hunt

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

Start Hunting!

Translated by