Counting consecutive negative numbers in an array
1 view (last 30 days)
Show older comments
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
0 Comments
Accepted Answer
Bruno Luong
on 24 Nov 2020
Edited: Bruno Luong
on 24 Nov 2020
length(strfind([false v<0],[0 1]))
or
sum(diff([false v<0])==1)
3 Comments
Bruno Luong
on 24 Nov 2020
Edited: Bruno Luong
on 24 Nov 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
More Answers (0)
See Also
Categories
Find more on Logical in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!