Set logical array values to true if value n values ahead is true
4 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
I'm curious if anyone can find a smart/clean way (other than a for loop of doing the following). Given a logical array x:
x = logical( [0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0] );
For n = 1 I'd like: y = [0, 0, 0, 1, 1, 0, 0, 0, 1, 1, 0, 0];
For n = 2 I'd like: y = [0, 0, 1, 1, 1, 0, 0, 1, 1, 1, 0, 0];
etc.
2 commentaires
Matt Fig
le 26 Nov 2012
Modifié(e) : Matt Fig
le 26 Nov 2012
Your examples conflict with your description. Do you want to place n values in the array ahead of a 1 as your examples show, or do you want to place a single 1 ahead of n 1s, as the title implies? If the latter, do you need to place a 1 only in front of exactly n 1s, or at least n 1s?
Réponse acceptée
Andrei Bobrov
le 26 Nov 2012
x = [0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0] > 0;
n = 2;
x(bsxfun(@minus,find(x),(n:-1:1)')) = true;
0 commentaires
Voir également
Catégories
En savoir plus sur Logical 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!