Counting consecutive negative numbers in an array

5 visualizaciones (últimos 30 días)
BN
BN el 24 de Nov. de 2020
Comentada: BN el 24 de Nov. de 2020
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

Respuesta aceptada

Bruno Luong
Bruno Luong el 24 de Nov. de 2020
Editada: Bruno Luong el 24 de Nov. de 2020
length(strfind([false v<0],[0 1]))
or
sum(diff([false v<0])==1)
  3 comentarios
Bruno Luong
Bruno Luong el 24 de Nov. de 2020
Editada: Bruno Luong el 24 de Nov. de 2020
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 el 24 de Nov. de 2020
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

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Multidimensional Arrays en Help Center y File Exchange.

Etiquetas

Productos


Versión

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by