Counting consecutive negative numbers in an array

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

 채택된 답변

Bruno Luong
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

BN
BN 2020년 11월 24일
Thank you...
I don't know why the answer is 2! I need 3. I think it doesn't count last -1 values. any idea?
Thank you so much again
Bruno Luong
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
BN
BN 2020년 11월 24일
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

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Matrix Indexing에 대해 자세히 알아보기

제품

릴리스

R2020a

태그

질문:

BN
2020년 11월 24일

댓글:

BN
2020년 11월 24일

Community Treasure Hunt

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

Start Hunting!

Translated by