Search code examples
matlabsearchmatrix-indexing

How to lookup an item by multiple indexes in matlab


I have a n+1 column matrix. I want a function mySearch(idx1,idx2...,idxn) that returns the n+1'th column in the row whose first n elements are equal to idx1...idxn

Next step, i want mySearch to return a value which is closest to the indexes, by some simple interpolation.

Is there an easy way to do this?

Thanks


Solution

  • Use norm to determine the distance and min to get the closest value:

    function v=mySearch( idx, M )
    n=length(idx);
    d=[]
    for row = M'
       d=[d; norm(row(1:n)-idx) ]
    end 
    [~, I]=min(d);
    v = M(I,n+1);
    end function
    

    Above idx is a vector of [idx1, idx2, ..., idxn].