How to count the numbers before consecutive negative values?
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
Tawsif Mostafiz
am 31 Jul. 2021
Kommentiert: Tawsif Mostafiz
am 31 Jul. 2021
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?
Akzeptierte Antwort
Image Analyst
am 31 Jul. 2021
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
0 Kommentare
Weitere Antworten (1)
Walter Roberson
am 31 Jul. 2021
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]
mask = c < 0
starts = strfind([0 mask], [0 1 1])
stops = strfind([mask 0], [1 1 0]) + 1
runlengths = stops - starts + 1
[maxrunlength, idx] = max(runlengths)
num_items_before_run = starts(idx) - 1
0 Kommentare
Siehe auch
Kategorien
Mehr zu Get Started with Image Processing Toolbox finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!