How to count the numbers before consecutive negative values?

Hi, I have a code, whose output is:
c =
0.4855 -0.1902 -0.1758 0.3935 -0.1264 -0.1274 -0.0914 -0.1149 -0.0930 0.5572 0.5098 1.0000
0.9737 0.9508 0.9046 0.9083 0.8997
Now, I want to calculate the maximum number of consecutive negative numbers (in this case, it is, 5) and the number of values before it (in this case, it is 4). How do I do it?

 채택된 답변

Image Analyst
Image Analyst 2021년 7월 31일
Try this:
mask = bwareafilt(c < 0, 1);
props = regionprops(mask, 'Area', 'PixelIdxList')
longestRun = props.Area % Will be 5
numbersBefore = props.PixelIdxList(1) - 1 % will be 4

추가 답변 (1개)

c = [
0.4855 -0.1902 -0.1758 0.3935 -0.1264 -0.1274 -0.0914 -0.1149 -0.0930 0.5572 0.5098 1.0000 ...
0.9737 0.9508 0.9046 0.9083 0.8997]
c = 1×17
0.4855 -0.1902 -0.1758 0.3935 -0.1264 -0.1274 -0.0914 -0.1149 -0.0930 0.5572 0.5098 1.0000 0.9737 0.9508 0.9046 0.9083 0.8997
mask = c < 0
mask = 1×17 logical array
0 1 1 0 1 1 1 1 1 0 0 0 0 0 0 0 0
starts = strfind([0 mask], [0 1 1])
starts = 1×2
2 5
stops = strfind([mask 0], [1 1 0]) + 1
stops = 1×2
3 9
runlengths = stops - starts + 1
runlengths = 1×2
2 5
[maxrunlength, idx] = max(runlengths)
maxrunlength = 5
idx = 2
num_items_before_run = starts(idx) - 1
num_items_before_run = 4

카테고리

도움말 센터File Exchange에서 Data Type Conversion에 대해 자세히 알아보기

제품

릴리스

R2021a

태그

질문:

2021년 7월 31일

댓글:

2021년 7월 31일

Community Treasure Hunt

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

Start Hunting!

Translated by