find first time where data crosses 1

조회 수: 26 (최근 30일)
Benjamin
Benjamin 2019년 4월 11일
댓글: Mark Sherstan 2019년 4월 11일
I have data stored in variables r and f. At some point the variable f goes above 1. How can I return the value of r the FIRST time f goes above 1? Interpolation of the r-value to find where f goes above 1 may be needed to be more precise. Any help would be greatly appreciated!

답변 (2개)

Mark Sherstan
Mark Sherstan 2019년 4월 11일
This example should help you with your data set
x = -2*pi:pi/12:2*pi;
y = 2*sin(x);
idx = find(y>1);
solution = idx(1)
fprintf("Solution at index %0.0f, x = %0.2f, y = %0.2f\n",solution,x(solution),y(solution))
figure(1)
hold on
plot(x,y)
plot(x(solution),y(solution),'*r')
  댓글 수: 2
Benjamin
Benjamin 2019년 4월 11일
편집: Benjamin 2019년 4월 11일
Does this actually interpolate though? I have this:
ix = find(y > 1, 1, 'first');
ix_r(count) = r(:,ix);
ix_r = ix_r';
but the problem is that it just returns r for when f exceeds the threshold of 1. How can I interpolate to find exactly where it would cross 1? then interpolate r
Mark Sherstan
Mark Sherstan 2019년 4월 11일
You can use the built in function interp1 as you have a region of interest.

댓글을 달려면 로그인하십시오.


Star Strider
Star Strider 2019년 4월 11일
I created ‘zci’ to do just that:
zci = @(v) find(v(:).*circshift(v(:), [-1 0]) <= 0); % Returns Approximate Zero-Crossing Indices Of Argument Vector
Then either use the interp1 function to do the interpolation, or create your own linear interpolation function:
x = 0:0.2:5; % Create Data
y = x.^2 - rand(size(x))*5; % Create Data
zci = @(v) find(v(:).*circshift(v(:), [-1 0]) <= 0); % Returns Approximate Zero-Crossing Indices Of Argument Vector
yzi = zci(y-1);
lintrp = [[x(yzi(1));x(yzi(1)+1)] ones(2,1)] \ [y(yzi(1));y(yzi(1)+1)] % Linear Interpolation
xint = (1-lintrp(2))/lintrp(1); % y=1 X-Value
figure
plot(x, y, xint, 1, '+')
grid
Experiment to get the result you want.

카테고리

Help CenterFile Exchange에서 Interpolation에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by