Effacer les filtres
Effacer les filtres

how to creat this vector?

2 vues (au cours des 30 derniers jours)
benghenia aek
benghenia aek le 29 Jan 2019
Modifié(e) : Stephen23 le 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]

Réponse acceptée

Stephen23
Stephen23 le 29 Jan 2019
Modifié(e) : Stephen23 le 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

Plus de réponses (2)

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

Catégories

En savoir plus sur Downloads dans Help Center et File Exchange

Tags

Aucun tag saisi pour le moment.

Community Treasure Hunt

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

Start Hunting!

Translated by