def prcntiles(x,percents):
    '''Equivalent to matlab prctile(x,p), uses linear interpolation.'''
    x=np.array(x).flatten()
    listx = np.sort(x)
    xpcts=[]
    lenlistx=len(listx)
    refs=[]
    for i in range(0,lenlistx):
        r=100*((.5+i)/lenlistx) #refs[i] is percentile of listx[i] in matrix x
        refs.append(r)

    rpcts=[]
    for p in percents:
        if p<refs[0]:
            rpcts.append(listx[0])
        elif p>refs[-1]:
            rpcts.append(listx[-1])
        else:
            for j in range(0,lenlistx): #lenlistx=len(refs)
                if refs[j]<=p and refs[j+1]>=p:
                    my=listx[j+1]-listx[j]
                    mx=refs[j+1]-refs[j]
                    m=my/mx #slope of line between points
                    rpcts.append((m*(p-refs[j]))+listx[j])
                    break
    xpcts.append(rpcts)
    return np.array(xpcts).transpose()