Counting consecutive negative numbers in an array
조회 수: 2 (최근 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!