[ADMB Users] Application of 'sort' function to variables

dave fournier otter at otter-rsch.com
Mon Apr 26 15:36:18 PDT 2010


As I said it is not too difficult to extend the
sort function to a dvar_matrix. I would use a dmatrix and sort that
keeping track of the where the rows end up.
something like this.


GLOBALS_SECTION
  #include <admodel.h>
  dvar_matrix sort(const dvar_matrix & M, int ic)
  {
    int rmin=M.indexmin();
    int rmax=M.indexmax();
    int cmin=M(rmin).indexmin();
    int cmax=M(rmax).indexmax();
    if (ic<cmin || ic>cmax)
    {
      cerr << "column index out of bounds in sort" << endl;
      ad_exit(1);
    }
    dmatrix CM(rmin,rmax,cmin,cmax+1);
    int i;
    for (i=rmin;i<=rmax;i++)
    {
      CM(i)(cmin,cmax)=value(M(i));
      CM(i,cmax+1)=i;   // keep track of the rows
    }
    dmatrix SCM=sort(CM,ic);
    dvector ind=column(SCM,cmax+1);
    dvar_matrix SM(rmin,rmax,cmin,cmax);
    for (i=rmin;i<=rmax;i++)
    {
      SM(i)=M(ind(i));
    }
    return SM;
  }

-- 



More information about the Users mailing list