# how to creat this vector?

2 views (last 30 days)
benghenia aek on 29 Jan 2019
Edited: Stephen23 on 29 Jan 2019
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]

Stephen23 on 29 Jan 2019
Edited: Stephen23 on 29 Jan 2019
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

Jan 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.

Torsten 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