Finding approximate y values for corresponding x values in matlab

조회 수: 29 (최근 30일)
Suppose I have a graph (formed from data) that has x and y values, but it is not analytic (so not continuous). I need to find a way such that for any x value, matlab will find the closest x value on the graph and give me the corresponding y value. Any ideas?
for example:
x y
1 2
2 5
3 8
4 13
5 23
6 49
So say that is my function, and I want to find the y value that corrisponds to x = 3.1 or 5.2, etc. The data is weird so I cant to a trivial linear approximation or something like that for the whole graph.

채택된 답변

Fangjun Jiang
Fangjun Jiang 2019년 5월 31일
interp1(x,y,3.1,'nearest')
  댓글 수: 2
Aleksandr Shmidt
Aleksandr Shmidt 2020년 8월 30일
just ran the snip in the code, 'nearest' will aproximate to the closest value in the data instead of finding value by the trend
Fangjun Jiang
Fangjun Jiang 2020년 8월 31일
That is what the OP wanted. "find the closest x value on the graph and give me the corresponding y value".

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

추가 답변 (1개)

Santiago Alcantara
Santiago Alcantara 2022년 2월 10일
A first aproach that you can use is the function "polyxpoly"( https://es.mathworks.com/help/map/ref/polyxpoly.html).
Another aproach is:
[val_1,idx_1]=min(abs(x-y))
This is usefull when the values are not exact values. You will find the minimun difference near to cero and exactly index to play with.

카테고리

Help CenterFile Exchange에서 Graph and Network Algorithms에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by