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

# st: RE: creat the matrix based on the condition

 From Joe Canner <[email protected]> To "[email protected]" <[email protected]> Subject st: RE: creat the matrix based on the condition Date Fri, 11 Apr 2014 14:19:05 +0000

```Wanhaiyou,

Here is a brute force way to do this.  Perhaps someone else has a more clever way.

***CODE***
mat A=J(3,6,0)
forvalues i=1/3 {
forvalues j=1/3 {
if (!mi(v`j'[`i'])) {
mat A[`i',v`j'[`i']]=1
}
}
}
matrix list A
***END****

I assume you want to generalize this for larger data sets (otherwise you could just create the matrix manually), which can easily be done with the following modifications:

***CODE***
mat A=J(_N,6,0)
forvalues i=1/`=_N' {
forvalues j=1/3 {
if (!mi(v`j'[`i'])) {
mat A[`i',v`j'[`i']]=1
}
}
}
matrix list A
***END****

This can also be generalized for more variables and a higher maximum column dimension, but that will take more work.

Regards,
Joe Canner
Johns Hopkins University School of Medicine

-----Original Message-----
From: [email protected] [mailto:[email protected]] On Behalf Of wanhaiyou
Sent: Friday, April 11, 2014 3:41 AM
To: [email protected]
Subject: st: creat the matrix based on the condition

Dear Statalisters,

I have three variables in the dataset as following

clear
input v1  v2  v3
1 3 5
3 4 6
1 2 .
end

I want to creat the linkage matrix based on these variables.
The result is as following

1 0 1 0 1 0
0 0 1 1 0 1
1 1 0 0 0 0

The max number is 6, so the dimension of matrix is 3*6

Thanks for all suggestions.

Bests,
wanhaiyou
Hunan University
--
Bests,

wanhaiyou

Hunan University
*
*   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/

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