How to cut out specific segments from a signal?

14 次查看(过去 30 天)
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

采纳的回答

Star Strider
Star Strider 2017-1-30
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 个评论

请先登录,再进行评论。

更多回答(0 个)

类别

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