# How to remove non consecutive values from a vector?

3 views (last 30 days)
GEORGIOS BEKAS on 15 Feb 2018
Commented: GEORGIOS BEKAS on 15 Feb 2018
I have the following vector that represents how many times consecutive values appear in a row by using the following code:
x = [5 3 3 1 0 9 9 4 4 4 4 5 1 2 2];
y=[]
for i = 1:length(x)
a = length(find(x(1:i)==x(i)));
y=[y,a];
end
and I want a code to remove the 2 2 subelements from the resultant y matrix.
y= [1 1 2 1 1 1 2 1 2 3 4 2 2 1 2]
##### 2 CommentsShowHide 1 older comment
GEORGIOS BEKAS on 15 Feb 2018
y= [1 1 2 1 1 1 2 1 2 3 4 1 1 1 2]

Birdman on 15 Feb 2018
y= [1 1 2 1 1 1 2 1 2 3 4 2 2 1 2]
idx=find(abs(diff(y))==2)+1;
y(idx:idx+1)=1
GEORGIOS BEKAS on 15 Feb 2018
correct. but aby non consecutive has the relationship: idx=find(abs(diff(y))>=2)+1; y(idx:idx+1)=1

### Categories

Find more on Loops and Conditional Statements 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!