Define function as interpolation of x,y data

↘锁芯ラ 提交于 2019-12-06 07:49:28

Assuming that you want some return value for numbers between the ones you have as reference, you can use linear interpolation:

    function y= linearLut(x)
         xl = [0 1 2 3 4 5];
         yl = [0 1 4 9 16 25];
         y = interp1(xl,yl,x);
    end

A more generic version of the function might be:

    function y= linearLut(xl,yl,x)
         y = interp1(xl,yl,x);
    end

And then you can create specific instances by using anonymous functions:

    f = @(x)(linearLut([0 1 2 3 4],[0 1 4 9 16],x));
    f(4);

You can import the file using textread(), then use find in order to select the right row.

Out of my head and untested:

function y = findinfile(x)
    m = textread(data.txt)
    ind = find(m(:,1)==x)
    y = m(ind,2)
end

If you only need to find the correct value in the array (without interpolation) you can use:

function out=ff(b)
  a = [0 1 2 3 4 5 ; 3 4 5 6 7 8]';
  [c,d]=find(a(:,1)==b);
  out = a(c,2);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!