# st: Matrix manipulation help

 From "Susan Olivia" To statalist@hsphsun2.harvard.edu Subject st: Matrix manipulation help Date Wed, 01 Apr 2009 16:11:57 -0700

```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 within its k-th nearest neighbors
value; and 0 otherwise.

I only can get to the point where replacing each cell of
matrix B to 1 if the value of the corresponding cell of
matrix A is < THRESHOLD. Below is what am currently
experimenting with. If I can get any tips how to efficiently
improve the current code and correctly handle this, I
greatly appreciate it.

Thanks,

Susan

********
local N=_N

mkmat var1-var100, matrix(DistMatrix)

matrix AAA=DistMatrix

mkmat _all, matrix(A)

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

mata: mata clear

mata:

void underbar(string scalar A,

real scalar thresh)

{

real matrix B

real scalar i

B = (st_matrix(A) :< thresh)

for(i=1; i<=rows(B); i++) {

B[i,i] = 0

}

st_matrix("B", B)

}

end

mat list A

mata: underbar ("A", 50)

matrix list B

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