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]

st: combination of k elements each being 0 or 1


From   "Impavido, Gregorio" <[email protected]>
To   "[email protected]" <[email protected]>
Subject   st: combination of k elements each being 0 or 1
Date   Tue, 4 Mar 2014 17:48:20 +0000

Hello.
	
I would like to define a matrix (2^k x k) with all possible combinations of k elements each being 0 or 1.  The code below produces what I want for k = 5 and can be modified for different values of k. My questions are:

- Is there a matrix function in STATA or MATA that generalizes the code below for variable k?
- If not, does anybody have suggestions on how to generalize the loop for variable k?

Thank you in advance for your help
Gregorio



******* begin example
clear
set more off
local x 5

local n = 2^`x'
set obs `n'

forvalues i = 1/`x' {
gen v`i' = .
}

* set trace on
local j = 1
while `j' < `n' {
forvalues i1 = 0/1 {
	forvalues i2 = 0/1 {
		forvalues i3 = 0/1 {
			forvalues i4 = 0/1 {
				forvalues i5 = 0/1 {
					forvalues l = 1/`x' {
						replace v`l' = `i`l'' in `j'
						}
						local j = `j' + 1
					}
				}
			}
		}
	}
}
******* end example

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