How to do matrix indexing in Stan

Hi guys,

I have to admit that I am new to Stan. I am planning to do linear interpolation in Stan, but I have problems in indexing a matrix in Stan. For example, I can easily locate the maximum and minimum value that is smaller/larger than the new x value and their corresponding y values, see the sample code below.

        ##x and its corresponding y values
        xy<-matrix(rep(seq(1,10,1),2),ncol=2)
        ##A new data point whose y value needs to be estimated
        new_x<-8.23
        ##Lower bound
        x1<-max(xy[xy[,1]<new_x,1])
        y1<-xy[xy[,1]==x1,2]
        ##Upper bound
        x2<-min(xy[xy[,1]>new_x,1])
        y2<-xy[xy[,1]==x2,2]
        ##new_y corresponds to new_x
        new_y<-(y2-y1)/(x2-x1)*(new_x-x1)+y1

However, how can I do it with function and generated quantities block in Stan? Many thanks!

Best,

Hongchao