How to cut out specific segments from a signal?
7 views (last 30 days)
Show older comments
Rafael Cordero
on 30 Jan 2017
Commented: Star Strider
on 18 Dec 2023
Hi all,
Im going nuts. This should be really easy but i've been stuck on it for days.
Let's say I have a signal (vector) x. There are certain parts of x I want removed/extracted so that I leave behind a shorter version of x, without these parts.
I have two vectors defining these unwanted parts. One that defines that start position (remove_start) and another the end position (remove_end).
So the vectors look like:
x = [x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x ...]
remove_start= [11 26 ...] remove_end= [15 31 ...]
I want to get rid of the bold segments in order to get x = [x x x x x x x x x x x x x x], so sticking the non bold bits together. 'x' can take any (real) number.
The problem arises from the fact that when I do something like x(remove_start(i):remove_end(i))=[]; I screw up the timings of remove_start and remove_end. I've tried so many different ways of doing this but none seem to work.
The current way im doing it is:
cut_lag=0;
for i=1:length(remove_start)
x(remove_start(i)-cut_lag(i):remove_end(i)-cut_lag(i))=[];
cut_lag(i+1)=cut_lag(i)+remove_end(i)-remove_start(i);
end
So the idea is to keep track of my cutting out the non-wanted bits has shifted the remaining remove times.
This seems to work for the start of x but it eventually degrades and it ends up cutting out the wrong parts...
Plsss helps, thanks so much!!
R
0 Comments
Accepted Answer
Star Strider
on 30 Jan 2017
If I understand correctly what you want to do, I would begin your loop at the end of the vector and move toward the beginning. That way, you don’t disrupt the other indices.
Something like this:
for i = length(remove_start):-1:1
I didn’t actually run your code, but this is the usual method of removing array elements in a loop without compromising the existing array references.
4 Comments
More Answers (0)
See Also
Categories
Find more on Logical in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!