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

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

 From wanhaiyou <[email protected]> To [email protected] Subject Re: st: RE: creat the matrix based on the condition Date Sat, 12 Apr 2014 10:39:38 +0800 (GMT+08:00)

```Dear Canner,

Bests,
wanhaiyou
Hunan University

> -----原始邮件-----
> 发件人: "Joe Canner" <[email protected]>
> 发送时间: 2014-04-11 22:19:05 (星期五)
> 收件人: "[email protected]" <[email protected]>
> 抄送:
> 主题: st: RE: creat the matrix based on the condition
>
> 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/

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