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

From |
"Susan Olivia" <olivia@primal.ucdavis.edu> |

To |
statalist@hsphsun2.harvard.edu |

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 <susan.olivia@gmail.com> To: Susan Olivia <olivia@primal.ucdavis.edu> Subject: Fwd: st: Matrix transformation Date: Fri, 3 Apr 2009 16:40:23 -0700 > ---------- Forwarded message ---------- > From: Glenn Goldsmith <glenn.goldsmith@gmail.com> > Date: Fri, Apr 3, 2009 at 4:08 PM > Subject: st: Matrix transformation > To: statalist@hsphsun2.harvard.edu > > > 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" <olivia@primal.ucdavis.edu> 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/

- Prev by Date:
**Re: st:reweight to compare two samples** - Next by Date:
**st: RE: specifying random effects in -xtmixed- for pretest/posttest clustered design** - Previous by thread:
**st: Matrix transformation** - Next by thread:
**st: maps (or plots) and labels with different colors** - Index(es):

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