How to calculate the number of consecutive negative values in an array before a positive appears?
2 views (last 30 days)
Show older comments
Tawsif Mostafiz
on 29 Jul 2021
Commented: Image Analyst
on 30 Jul 2021
I am writing a code, where the output is as follows:
c =
-0.4607 -0.4659 -0.5165 -0.5168 -0.5164 -0.4626 -0.4668 -0.3730 -0.3426 0.3224
0.3251 0.3270 0.3281 0.3400 -0.1816 -0.1830 -0.1851 1
I want to calculate the number of negative values before a positive value appeares for each cases. How do I do that?
0 Comments
Accepted Answer
Image Analyst
on 29 Jul 2021
Here's another way:
c =[-0.4607 -0.4659 -0.5165 -0.5168 -0.5164 -0.4626 -0.4668 -0.3730 -0.3426 0.3224 ...
0.3251 0.3270 0.3281 0.3400 -0.1816 -0.1830 -0.1851 1]
props = regionprops(c<0, 'Area'); % Measure lengths of all runs of negative values.
results = [props.Area] % [9, 3] % Turn from structure into a vector.
3 Comments
Image Analyst
on 30 Jul 2021
That number is simply the location minus 1. So it finds 14 at location 2 for the max, and there is one number, 1, before that location.
More Answers (1)
Adam Danz
on 29 Jul 2021
c = [-0.4607 -0.4659 -0.5165 -0.5168 -0.5164 -0.4626 -0.4668 -0.3730 -0.3426 0.3224 ,...
0.3251 0.3270 0.3281 0.3400 -0.1816 -0.1830 -0.1851 1];
A = c(:)<0;
B = diff(find([0,A(:).',0]==0))-1;
B(B==0) = []
0 Comments
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!