How do I count the number of times my graph crosses two separate lines?
Show older comments
I have this graph:

I want to count the number of times that the graph goes from y=-0.08 (the bottom line) and y=0.16 (the top line). If it goes up and down inbetween those two lines, I don't care. I just want it to count every time it hits -0.08 and goes back to 0.16. For example, in the graph below, count should equal 3 (1 between 18.5 and 19 seconds; 1 between 19.5 and 20 seconds; 1 between 20.5 and 21 seconds; and it shouldn't count the tiny maximum peak at about 19.75 because the graph hadn't yet reached the bottom line again)

(I've seen this done with just a minimum or just a maximum but I don't know how to get it to only count from the bottom line to the top line)
1 Comment
Dyuman Joshi
on 2 Apr 2024
Edited: Dyuman Joshi
on 2 Apr 2024
Please share what you have attempted yet.
Accepted Answer
More Answers (1)
maybe too caomplicated, but this can work:
data=[1:10 9:-1:1 2:10 9:-1:1 2:10]; % row vector to not get problems with num2str later
lowThr=3;
highThr=6;
% remove data between those thr as those are not interesting
data(data>=lowThr & data<=highThr)=[];
% create 1 if it is under lowThr and 2 of it is bigger than highThr
asBin=(data<lowThr)*1 + (data>highThr)*2
% to string without whitespace
asStr=erase(num2str(asBin),' ')
% remove duplicates
asStr=regexprep(asStr,'1+','1')
asStr=regexprep(asStr,'2+','2')
% if you want to count pairs of "under threshold and then over threshold"
numel(strfind(asStr,'21'))
% or vice versa
numel(strfind(asStr,'12'))
Categories
Find more on Surface and Mesh Plots in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!


