Bookmark and Share

Notice: On March 31, it was announced that Statalist is moving from an email list to a forum. The old list will shut down on April 23, and its replacement, is already up and running.

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

st: Mata - extracting various vectors of different sizes in one loop

From   nick bungy <>
To   "" <>
Subject   st: Mata - extracting various vectors of different sizes in one loop
Date   Thu, 4 Apr 2013 11:26:08 +0100

I have a mata code that cycles through grid references (eastings, northings) of x entities and calculates for each entity all the other entities which are within a 10km radius of it.
So each individual entity has a row vector, with dimensions anywhere between 1 row (1 firm within 10km radius) and ~80 rows (80 firms within 10km radius). This is throwing up conformity errors when I try to store these vectors into a selection of ~80 variables in Stata.
My thought was to artifically inflate all row vectors to say 100 and fill all of the extra cells in each row vector with 0, then I can extract to 100 variables without conformity errors. I can then clean this up quite easily using Stata functions. I'm not quite sure how to go about this though. 
My mata code is the following:

         geoeasta = st_data(., "Geoeast")           
         geonortha = st_data(., "Geonorth") 
         n = rows(geoeasta)
   density = .
   densitytwo = .
   densitythree = .
   dups   = .
         for(i=1; i<=n; ++i) {
                 d = sqrt((geoeasta:-geoeasta[i]):^2 + (geonortha:-geonortha[i]):^2)
                 d[i] = .
                 density = select(d, d[.,1]:<10000)
                 minindex(density, 80, densitytwo, dups)
                 st_store(i, ("MSOA1", "MSOA2", "MSOA3" etc etc.), densitytwo)
     //This stores the nearest neighbours into our variables, which we defined at the top.
I suspect I need a line or two below minidex, which inflates densitytwo to a 100 row vector and fills all the extra rows generated with 0. Or perhaps there is a more elegent way?
Many thanks,
*   For searches and help try:

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