Statalist


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Fwd: st: Matrix transformation


From   "Susan Olivia" <[email protected]>
To   [email protected]
Subject   Re: Fwd: st: Matrix transformation
Date   Fri, 03 Apr 2009 16:57:42 -0700

Thanks heaps Glenn for the programming tips. It's doing
exactly what I after. 

Susan

----- Original Message Follows -----
From: Susan Olivia <[email protected]>
To: Susan Olivia <[email protected]>
Subject: Fwd: st: Matrix transformation
Date: Fri, 3 Apr 2009 16:40:23 -0700

> ---------- Forwarded message ----------
> From: Glenn Goldsmith <[email protected]>
> Date: Fri, Apr 3, 2009 at 4:08 PM
> Subject: st: Matrix transformation
> To: [email protected]
> 
> 
> Hi Susan,
> 
> As far as I'm aware Stata/Mata don't really do sparse
> matrix storage, but you can otherwise replicate the
> behavior of sparse() with the following mata function:
> 
> ********* Begin code **************
> mata:
> real matrix sparse(real matrix x)
> {
>  real matrix y
>  real scalar k
> 
>  y = J(colmax(x[,1]),colmax(x[,2]),0)
>  for (k=1; k<=rows(x); k++) {
>    y[x[k,1],x[k,2]] = y[x[k,1],x[k,2]] + x[k,3]
>  }
> 
>  return(y)
> }
> end
> ********* End code ****************
> 
> You can then call this either from within mata:
> 
>  y = sparse(x)
> 
> or within stata:
> 
>  mata : st_matrix("y",sparse(st_matrix("x")))
> 
> (in both cases assuming that your input matrix is called x
> , and output matrix is called y).
> 
> HTH,
> 
> Glenn
> 
> 
> "Susan Olivia" <[email protected]> wrote:
> 
> Dear all,
> 
> I would like to transform a say 4272 x 3 matrix (this
> matrix basically tells us the distance between each pairs
> of observation) onto 1068 x 1068.
> 
> In MATLAB, this can be transformed using the following
> command:
> 
> load GIS_4NN.txt;
> W4NN = sparse(GIS_4NN(:,1), GIS_4NN(:,2), GIS_4NN(:,3));
> 
> Is there any equivalent command like the above in STATA?
> 
> Any tips on this much appreciated.
> 
> Thanks,
> 
> Susan
> 
> *
> *   For searches and help try:
> *   http://www.stata.com/help.cgi?search
> *   http://www.stata.com/support/statalist/faq
> *   http://www.ats.ucla.edu/stat/stata/
> 
*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/



© Copyright 1996–2024 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index