Effacer les filtres
Effacer les filtres

Identifying sequences of two or more and maintaining vector shape

2 vues (au cours des 30 derniers jours)
MohammedLee Lee
MohammedLee Lee le 6 Déc 2013
Modifié(e) : Andrei Bobrov le 6 Déc 2013
I'm pretty new to Matlab and know what I want to do, just not how to code it. I have a vector in which I want to remove nonzero values which are not in a sequence of two or more i.e.
0 112 58 0 0 0 0 114 0 24 0 0
and turn the 114 and 24 values to 0, but keep any value sequences of two or more.
I've switched to logicals to keep things simple and managed to find out how many times there are these sequences:
ML4= find(diff([-1 ML3 -1]) ~= 0);
runlength = diff(ML4) ;
runlength1 = runlength(1+(ML3(1)==0):2:end);
numberBursts=sum(runlength1>1);
but not a way to keep the original vector shape Any help much appreciated.

Réponse acceptée

Andrei Bobrov
Andrei Bobrov le 6 Déc 2013
Modifié(e) : Andrei Bobrov le 6 Déc 2013
x = [0 112 58 0 0 0 0 114 0 24 0 0];
x(strfind([0,x(:)' ~= 0, 0],[0 1 0])) = 0;

Plus de réponses (1)

Jos (10584)
Jos (10584) le 6 Déc 2013
x = [0 112 58 0 0 0 0 114 0 24 0 0]
xx = [false x~=0 false]
ix = 2:numel(x)+1
tf = ~xx(ix-1) & xx(ix) & ~xx(ix+1)
y = x
y(tf) = 0

Catégories

En savoir plus sur MATLAB dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by