【语言转换】matlab prctile方法IDL实现

来源:互联网 发布:淘宝app 商品列表 编辑:程序博客网 时间:2024/06/12 09:05

经过20天断断续续的敲代码,终于翻译完了一坨matlab代码,每次遇到matlab自己的函数,就要深呼吸一下~好在都实现了。。。

这篇博文是prctile方法,求百分位数,详情参考帮助文档,以下是IDL写的方法,实现这个。几经修改才去掉一些与Matlab不符的测试结果,测试数据就是前面隐掉的部分。

FUNCTION percentile,data,values;matlab prctile;pro percentile  ;data=[[2,2,2],[4,4,4],[6,6,6]]  ;data=[2,4,6]  ;Temp=[1,2,3,4,5]  ;tdata=[0,1,2,3,4,5,4,3,2,1,2,3,4,5,6,3,3,3,3,3]  ;data=transpose(tdata)  ;values=100/7.      value=values/100.;转换百分比  ;检查value合法  if(not keyword_set(value)) then begin    print,'ERROR values'    ;return,-1  endif  ;if size(value,/Dimensions) lt 1 then begin  ;  print,'ERROR values'    ;return,-1  ;endif    if size(data,/N_DIMENSIONS) ge 1 then begin;检查data合法    n=n_elements(data[0,*]);the number of rows of data is equal to the number of percentiles required    clu = n_elements(data[*,0]);The ith row of data contains the values(i) percentiles of each column of X    result=dblarr(clu,n_elements(value))    for k=0,clu-1,1 do begin;循环列      subdata=dblarr(n);将一列的值提出来      for j=0,n-1,1 do begin        subdata[j]=data[k,j]      endfor      idx = sort(subdata)      ;print,subdata[idx]      dper = dblarr(n);百分位数      for i=0,n-1,1 do begin        dper[i]=(0.5+i)/(n*1.)      endfor      for i=0,n_elements(value)-1,1 do begin;循环求百分比值        if value[i] ge dper[n-1] then begin          result[k,i] = subdata[idx[n-1]]        endif else begin          if value[i] le dper[0] then begin            result[k,i] = subdata[idx[0]]          endif else begin            bltmp=0            for h=0, n-2 do begin              if value[i] gt dper[h] && value[i] lt dper[h+1] then begin                ind = h+1                break              endif              if value[i] eq dper[h] then begin                ;ind = h                result[k,i]=dper[h]                bltmp=1                break              endif                             endfor            if bltmp eq 1 then begin              continue            endif            vdown=subdata(idx(ind-1))            vup=subdata(idx(ind))            ddown=dper(ind-1)            dup=dper(ind)            result[k,i] = vdown+(value[i]-ddown)*(vup-vdown)/((dup-ddown)*1.);按公式赋值            if result[k,i] gt vup then begin;去掉误差现象              result[k,i] = vup            endif          endelse        endelse      endfor      idx = TEMPORARY(idx)      dper = TEMPORARY(dper)      subdata=TEMPORARY(subdata)    endfor  endif else begin    print,'ERROR data'    ;return,-1  endelse    return,result  ;print,resultend


原创粉丝点击