フィルターのクリア

How count positive numbers, but when you get a negative, the sum stops. so then, sum the following sequence of positive numbers..

1 回表示 (過去 30 日間)
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

採用された回答

Andrei Bobrov
Andrei Bobrov 2012 年 10 月 13 日
編集済み: Andrei Bobrov 2012 年 10 月 13 日
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 件のコメント
Javier
Javier 2012 年 10 月 13 日
編集済み: Javier 2012 年 10 月 13 日
Thanks
Andrei Bobrov
You are the Best ¡

サインインしてコメントする。

その他の回答 (2 件)

Wayne King
Wayne King 2012 年 10 月 13 日
編集済み: Wayne King 2012 年 10 月 13 日
x = randn(100,1);
sum(x(x>0))
For an example with integers:
x = randi([-5 5],20,1);
sum(x(x>0))
  1 件のコメント
Javier
Javier 2012 年 10 月 13 日
Thanks, but I just want to sum the sequence of positive numbers that are together, not all positive
Regards Claudio

サインインしてコメントする。


Walter Roberson
Walter Roberson 2012 年 10 月 13 日
sum(x(1 : find(x <= 0, 1)))
or
sum(x .* cumprod(x > 0))
  1 件のコメント
Javier
Javier 2012 年 10 月 13 日
編集済み: Javier 2012 年 10 月 13 日
Thanks Walter, but I just want to sum the sequence of positive numbers that are together
Example
v =
1 1 -1 1 1 1 -1 -1 1
%sum the sequence of positive numbers that are together
sol =
2 3 1

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeConvert Image Type についてさらに検索

タグ

タグが未入力です。

Community Treasure Hunt

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

Start Hunting!

Translated by