# st: Matrix manipulation

 From "Susan Olivia" To statalist@hsphsun2.harvard.edu Subject st: Matrix manipulation Date Thu, 11 Dec 2008 07:19:03 -0800

```Dear all,

I would like to create a binary matrix (say matrix B) using
the existing information from current matrix A.

Specifically, for each cell (i,j) of matrix B, I want to
replace its value with 1 if the value of the corresponding
cell (i,j) of matrix A is less than the specified threshold
value. In other words, matrix B[i,j] = 1 if A[i,j] <
THRESHOLD.

Below is the attempt that I tried in STATA, but it didn't
work. Something must be wrong with my code. If I can get any
tips how to efficiently and correctly handle this, I greatly
appreciate it.

Thanks,

Susan

*** Create matrix A from current data file

local N=_N
mkmat _all, matrix(_A)

*** Create a temp matrix filled with 0s ***

matrix _B=J(`N',`N',0)

*** Specify the threshold distance ***

gen threshold = 500

*** Re-place B[i,j] = 1 if A[i,j] < threshold

local i=1
while `i' <=`N'{
local j = `i'+1
while `j'<=`N'{
matrix _T[`i',`j']= _A
matrix _T[`j',`i']= _A
if _A < threshold
matrix _B[`i',`j']=1
matrix _B[`j',`i']=1

}
}
local j = `j'+1

}
local i = `i'+1

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