Bookmark and Share

Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.


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

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


From   Matthew Baker <[email protected]>
To   [email protected]
Subject   Re: st: Mata - extracting various vectors of different sizes in one loop
Date   Thu, 4 Apr 2013 09:04:05 -0400

Nicholas --

I have run into this problem as well, but have a slightly different
solution (I'm not sure it's more elegant, though!) One thing that
wasn't quite clear from your message was whether or not the values you
were computing had to be placed in a particular place or if they could
just be listed "from the beginning of a row," so I assumed not.

My solution is to create a matrix with a maximal number of columns and
then fill it in on the fly, and then get everything into stata using
st_view - which has the property that variables can be overwritten
from within mata. Anyways:

clear all
set seed 8675309
set obs 100
mata:

// Random lengths for vectors:
ni=J(100,1,1)
for (i=1;i<=100;i++) ni[i]=round(runiform(1,1)*20)+1

// Random vectors of random lengths placed in the array,
// Which will also store a scalar which is length:

Z=J(100,30,.)
for (i=1;i<=100;i++) {
Stuff=runiform(1,ni[i])
length=cols(Stuff)
Z[i,1::length]=Stuff
}
end

local zvals
forvalues i=1/30 {
gen z`i'=.
local zvals "`zvals' z`i'"
}
mata
st_view(Ztemp=.,.,"`zvals'")
Ztemp[.,.]=Z
end

On Thu, Apr 4, 2013 at 6:26 AM, nick bungy <[email protected]> wrote:
> 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:
>
> mata:
>          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.
>
>   }
>
>
>   end
> 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,
> Nicholas
> *
> *   For searches and help try:
> *   http://www.stata.com/help.cgi?search
> *   http://www.stata.com/support/faqs/resources/statalist-faq/
> *   http://www.ats.ucla.edu/stat/stata/



-- 
Dr. Matthew J. Baker
Department of Economics
Hunter College and the Graduate Center, CUNY

*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/faqs/resources/statalist-faq/
*   http://www.ats.ucla.edu/stat/stata/


© Copyright 1996–2018 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   Site index