Question on using interpolate function

조회 수: 4 (최근 30일)
Deepa Maheshvare
Deepa Maheshvare 2020년 1월 29일
댓글: Sindar 2020년 1월 29일
This is a follow-up to my previous post here. I want to find the x value at which data has 5.0 (first occurence).
I tried using the find function suggested in the comments.
l = load('var.mat'); % file attached
l = l.unnamed;
x = l(:,1);
data = l(:,2);
tsinterp = getinterpmethod(x)
xmatch = x(find(data == 5.0,1))
The answer that I obtain is 1.71. However, the first occurrence of 5.0 is at 0.20 (x value). I'm not sure what's going wrong.
Any suggestions on how to proceed will be really helpful

채택된 답변

Sindar
Sindar 2020년 1월 29일
Check your data again. "data" is slightly less than 5 until x=0.485, and the first point exactly equal to 5 is x=1.71. You could add a tolerance like so:
tol = 1e-4;
xmatch = x(find(data >= 5.0-tol,1,'first'))
  댓글 수: 2
Deepa Maheshvare
Deepa Maheshvare 2020년 1월 29일
Hi, Many thanks for the response. Could you please explain why 1,'first') is required? I understand first picks the first occurrence. Could you please explain why 1 is required?
Sindar
Sindar 2020년 1월 29일
"1" tells the number of occurences you want, in case there are multiple. 'first' is the direction; it isn't required, since it is the default, but I like to include it out of habit

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

추가 답변 (1개)

Walter Roberson
Walter Roberson 2020년 1월 29일
See ismembertol()
The data that is stored corresponding to time 0.20, is not bit-for-bit identical to the representation for double precision 5.0. If you were to subtract 5 from the content at that location you would get something not exactly 0.

카테고리

Help CenterFile Exchange에서 Resizing and Reshaping Matrices에 대해 자세히 알아보기

제품


릴리스

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by