Counting consecutive negative numbers in an array

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

 Risposta accettata

Bruno Luong
Bruno Luong il 24 Nov 2020
Modificato: Bruno Luong il 24 Nov 2020
length(strfind([false v<0],[0 1]))
or
sum(diff([false v<0])==1)

3 Commenti

Thank you...
I don't know why the answer is 2! I need 3. I think it doesn't count last -1 values. any idea?
Thank you so much again
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
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

Accedi per commentare.

Più risposte (0)

Prodotti

Release

R2020a

Tag

Richiesto:

BN
il 24 Nov 2020

Commentato:

BN
il 24 Nov 2020

Community Treasure Hunt

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

Start Hunting!

Translated by