how to creat this vector?

2 views (last 30 days)
benghenia aek
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]

Accepted Answer

Stephen23
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

More Answers (2)

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

Categories

Find more on Downloads in Help Center and File Exchange

Tags

No tags entered yet.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!