[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

st: Matrix manipulation

From   "Susan Olivia" <>
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] <

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. 



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

© Copyright 1996–2017 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index