Statalist


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

st: Re: Matrix manipulation help


From   "Martin Weiss" <[email protected]>
To   <[email protected]>
Subject   st: Re: Matrix manipulation help
Date   Wed, 13 May 2009 20:03:17 +0200

<>

For a start, the # of obs is accessible via "c(N)" throughout your journey... No need to put it in a -local- or -scalar-

This -for- loop you are using used to be popular, as I understand from old SJ articles, but has since been superseded by -foreach- and - forvalues-. See http://www.stata-journal.com/sjpdf.html?articlenum=pr0005

It lives on in -mata-, though, but you employ the old -matrix- language...


HTH
Martin
_______________________
----- Original Message ----- From: "Susan Olivia" <[email protected]>
To: <[email protected]>
Sent: Wednesday, May 13, 2009 7:53 PM
Subject: st: Matrix manipulation help


Dear Stata list,

Can I get programming advice how to create a NxN distance
matrix using the information on latitude and longitude.

Below is what I attempted to do in STATA, but it didn't
work. Would be great if I can get any advice on how to
improve this code.

Thanks,

Susan

****************************
qui set obs 1000

gen xcoord = uniform()*100
gen ycoord = uniform()*100

mkmat xcoord ycoord

local N=_N
scalar N = _N

matrix latlong= xcoord,ycoord

matrix D = J(`N',`N', 0)

* looping through the rows and columns of the matrix D, to
get distance pair for each observations

 for(i = 1; i <=N; i++) {
      for(j = 1; j<=N; j++) {
           if (i!=j){
           D[i,j]=
((xcoord[i]-xcoord[j]^2+(ycoord[i]-ycoord[j]^2)^0.5

           }
        }
}
*
*   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