How can I count the number of 1 after every element of the vector?
1 vue (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Nicolò Castellani
le 17 Fév 2020
Modifié(e) : Stephen23
le 17 Fév 2020
I have this vector of 1 and 0:
X= [1 1 1 1 0 0 1 1 0 1 0 1 0 0 1 1]
I want to create a vector Y that counts for every element of X the number of 1 that there are after it (including the 1 of the element, if is 1)
So the vector Y should be:
Y=[4 3 2 1 0 0 2 1 0 1 0 1 0 0 2 1]
How can I do that?
thank you!
0 commentaires
Réponse acceptée
Stephen23
le 17 Fév 2020
Modifié(e) : Stephen23
le 17 Fév 2020
>> X = [1,1,1,1,0,0,1,1,0,1,0,1,0,0,1,1]
X =
1 1 1 1 0 0 1 1 0 1 0 1 0 0 1 1
>> Z = cumsum([true,diff(X)>0]);
>> F = @(v){flip(cumsum(flip(v)))};
>> Y = cell2mat(accumarray(Z(:),X(:),[],F)).'
Y =
4 3 2 1 0 0 2 1 0 1 0 1 0 0 2 1
0 commentaires
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur NaNs 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!