Counting consecutive negative numbers in an array
3 次查看(过去 30 天)
显示 更早的评论
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 个评论
采纳的回答
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
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
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Logical 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!