How count positive numbers, but when you get a negative, the sum stops. so then, sum the following sequence of positive numbers..
1 vue (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Example
v =
1 1 -1 1 1 1 -1 -1 1
%sum the sequence of positive numbers that are together
sol =
2 3 1
Regards Claudio
0 commentaires
Réponse acceptée
Andrei Bobrov
le 13 Oct 2012
Modifié(e) : Andrei Bobrov
le 13 Oct 2012
v = [1 1 -1 1 1 1 -1 -1 1];
z = v > 0;
id = find([true;diff(v.') ~= 0]);
k = diff([id;numel(v)+1]);
out = k(z(id));
or use Image Processing Toolbox
v = [1 1 -1 1 1 1 -1 -1 1]
z = v > 0;
s = regionprops(z, 'Area');;
out = [s.Area];
or
v = [1 1 -1 1 1 1 -1 -1 1];
v1 = [-v(1)*sign(v(1)),v,-v(end)*sign(v(end))];
out = strfind(v1,[1, -1])-strfind(v1,[-1 1]);
1 commentaire
Plus de réponses (2)
Wayne King
le 13 Oct 2012
Modifié(e) : Wayne King
le 13 Oct 2012
x = randn(100,1);
sum(x(x>0))
For an example with integers:
x = randi([-5 5],20,1);
sum(x(x>0))
Voir également
Catégories
En savoir plus sur Particle & Nuclear Physics 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!