필터 지우기
필터 지우기

How can you create event points with continuous data?

조회 수: 2 (최근 30일)
Beck Hunter
Beck Hunter 2021년 6월 24일
댓글: Beck Hunter 2021년 7월 9일
Hello!
I have made a simpler example to help explain what I am trying to do:-
Lets say you have a time colum vector corresponding to another colum vector y. A plot of this as a graph can be seen in the image attached.
time = [1;2;3;4;5;6;7];
y = [-1;3;4;-2;-3;1;2];
plot(time,y);
I am trying to work out how I would be able to plot the xline (a vertical line) at the point in which the y-value has been negative for two time steps. I have illustrated this on the graph attached.
Is there a way to do this? I am hoping to apply this logic to more complicated graphs in order to automatically determine events occuring.
I have looked at creating a vector such as below and using the movsum function to try and create an if function for two steps being negative, but I am having trouble getting this to work.
mat = [1;1;1;1;1;1;1];
matsum = movsum(mat,2);
Thank you in advance for any insight!

채택된 답변

Sean Brennan
Sean Brennan 2021년 6월 24일
Here's a quick method to do this using diff. It's possible to put this all into one compact line, but left it "spread out" here in order to make the steps more clear. Hope this answers your question!
% Use differences to spot change in signs
diff_y = [0; diff(y)];
same_sign_repeated = [0; (diff_y(1:end-1).*diff_y(2:end))>0];
twice_negative = same_sign_repeated.*(y<0);
xline(time(twice_negative>0));

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 2-D and 3-D Plots에 대해 자세히 알아보기

제품


릴리스

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by