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 and subsets...


From   Austin Nichols <[email protected]>
To   [email protected]
Subject   Re: st: Mata and subsets...
Date   Thu, 1 Mar 2012 17:21:16 -0500

Matthew Baker <[email protected]>
Note the Stata FAQ tells you to give the provenance as -moremata- on SSC.

Here is an unpleasantly Stataish (as opposed to Mataish) solution,
with the looping you hope to avoid:

cap ssc inst moremata
mata
transmorphic matrix cosubsets(n,k) {
 m1=mm_subsets(n,k)
 c=runningsum(J(1,n,1))
st_local("c",invtokens(strofreal(c)))
st_local("d",invtokens(strofreal(m1'[1,.])))
stata("loc e: list c-d")
m2=tokens(st_local("e"))'
 for (i=2; i<=cols(m1); i++) {
st_local("d",invtokens(strofreal(m1'[i,.])))
stata("loc e: list c-d")
m2=m2,tokens(st_local("e"))'
 }
return(m2)
}
mm_subsets(5,2)
cosubsets(5,2)


On Thu, Mar 1, 2012 at 3:03 PM, Matthew Baker
<[email protected]> wrote:
> Dear Listers --
>
> I have a question about obtaining subsets and their complements in
> mata. To be clear, what I am trying to do is get a listing of both the
> subsets and their corresponding complements.
>
> For example,
> mata
> mm_subsets(4,2)
>
> gives me the subsets:
>       1   2   3   4   5   6
>    +-------------------------+
>  1 |  1   1   1   2   2   3  |
>  2 |  2   3   4   3   4   4  |
>    +-------------------------+
>
> But the question is: is there also an (efficient, general) way to
> obtain the corresponding complements:
>
>       1   2   3   4   5   6
>    +-------------------------+
>  1 |  3   2   2   1   1   1  |
>  2 |  4   4   3   4   3   2  |
>    +-------------------------+
>
> I'm hoping for a general method that doesn't depend upon having four
> items or the two-element subsets but am baffled about how to do this
> without looping, etc.
>
> Thanks in advance!
>
> Matt Baker

*
*   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–2018 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   Site index