MATLAB Answers

Counting consecutive negative numbers in an array

4 views (last 30 days)
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

Accepted Answer

Bruno Luong
Bruno Luong on 24 Nov 2020
Edited: Bruno Luong on 24 Nov 2020
length(strfind([false v<0],[0 1]))
sum(diff([false v<0])==1)
Behzad Navidi
Behzad Navidi on 24 Nov 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

Sign in to comment.

More Answers (0)





Community Treasure Hunt

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

Start Hunting!