Counting consecutive negative numbers in an array

5 ビュー (過去 30 日間)
BN
BN 2020 年 11 月 24 日
コメント済み: BN 2020 年 11 月 24 日
Hello, I need to find how many times consecutive negative numbers are in the array. For example:
v = [11 2 3 -1 -2 1 -1 -1 -3 1 3 -1];
The answer must be: 3
Thank you

採用された回答

Bruno Luong
Bruno Luong 2020 年 11 月 24 日
編集済み: Bruno Luong 2020 年 11 月 24 日
length(strfind([false v<0],[0 1]))
or
sum(diff([false v<0])==1)
  3 件のコメント
Bruno Luong
Bruno Luong 2020 年 11 月 24 日
編集済み: Bruno Luong 2020 年 11 月 24 日
How do you get 2?
>> v= [11 2 3 -1 -2 1 -1 -1 -3 1 3 -1]
v =
11 2 3 -1 -2 1 -1 -1 -3 1 3 -1
>> length(strfind([false v<0],[0 1]))
ans =
3
>> sum(diff([false v<0])==1)
ans =
3
BN
BN 2020 年 11 月 24 日
Thank you so much. I forgot to use the clear command then try the code. After that, your code works very well.
Thank you it is really helpful for me

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeMultidimensional Arrays についてさらに検索

タグ

製品


リリース

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by