MATLAB Answers

## how to creat this vector?

Asked by benghenia aek

### benghenia aek (view profile)

on 29 Jan 2019
Latest activity Edited by Stephen Cobeldick

### Stephen Cobeldick (view profile)

on 29 Jan 2019
Accepted Answer by Stephen Cobeldick

### Stephen Cobeldick (view profile)

hello everyone
I have vector
X=[1 0 0 1 1 0 1 1 1 1 0 0 0 1 1 0 1 1 1 1 1 0]
i need this transformation
if Nbr of 1 >3 the vector become:
Y=[0 0 0 0 0 0 1 1 1 1 0 0 0 0 0 0 1 1 1 1 1 0]

#### 0 Comments

Sign in to comment.

### Tags

No tags entered yet.

## 3 Answers

### Stephen Cobeldick (view profile)

Answer by Stephen Cobeldick

### Stephen Cobeldick (view profile)

on 29 Jan 2019
Edited by Stephen Cobeldick

### Stephen Cobeldick (view profile)

on 29 Jan 2019
Accepted Answer

An easy solution using a loop:
>> X = [1,0,0,1,1,0,1,1,1,1,0,0,0,1,1,0,1,1,1,1,1,0]
X =
1 0 0 1 1 0 1 1 1 1 0 0 0 1 1 0 1 1 1 1 1 0
D = diff([0,X,0]);
B = find(D>0);
E = find(D<0)-1;
N = 3;
for k = 1:numel(B)
if (E(k)-B(k))<N
X(B(k):E(k)) = 0;
end
end
>> X
X =
0 0 0 0 0 0 1 1 1 1 0 0 0 0 0 0 1 1 1 1 1 0

#### 0 Comments

Sign in to comment.

Answer by Jan

### Jan (view profile)

on 29 Jan 2019

The question is not clear, but I assume you mean: set values to 0, if less than 3 neighboring elements are 1.
X = [1 0 0 1 1 0 1 1 1 1 0 0 0 1 1 0 1 1 1 1 1 0];
[B, N] = RunLength(X);
B(N < 3) = 0;
Y = RunLength(B, N);
If you do not have a compiler installed, use RunLength_M from the same submission.

#### 0 Comments

Sign in to comment.

### Torsten (view profile)

Answer by Torsten

### Torsten (view profile)

on 29 Jan 2019

X=[1 0 0 1 1 0 1 1 1 1 0 0 0 1 1 0 1 1 1 1 1 0];
if sum(X)>3
X=[0 0 0 0 0 0 1 1 1 1 0 0 0 0 0 0 1 1 1 1 1 0];
end

#### 0 Comments

Sign in to comment.