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

st: re matrix substitution

From   Marwan Elkhoury <>
Subject   st: re matrix substitution
Date   Sat, 22 Aug 2009 11:52:14 +0200

dear statalist,

sorry to bother you with this minor pblm but I'm not familiar with the  
stata mata environment , more familiar with the matlab matrix  
environment.  anyway, I wish to substitute some matrix factor loadings  
which are negative or small to zero and keep the others identical.
Now, I've written naively some piece of codes to do that and it's not  
working, either because I'm not writing the pgm correctly or not  
saving it correctly in the right place or not calling the function  
correctly; so the codes are for stata 10 version and are the following:

/* matrix substitution */

  program mysubsi
                 version 10
                 syntax varname [if] [in]
		    marksample touse
                 mata: mysubsi(string st_matrix("A"), "`touse'")
                 display as txt "  C = " as res r(C)

capture mata mata drop mysubsi()
real matrix mysubsi(real matrix A, real scalar thresh)
   real scalar r, c
   real matrix C
     for(i=1;i<=r;i++) {
       for(j=1;j<=c;j++) {
	if (A[i,j]< thresh) {
	C[i,j] =0
else {
	C[i,j] = A[i,j]

mata mosave mysubsi(), dir(c:\ado\personal\m) replace

/// end of pgm

then I try to call the function, writing:
mata: mysubsi("A", "`touse'")
mata: mysubsi("entering the real matrix function which I want to  
perform change upon", thresh number)

I'm surely missing on something and any help would be greatly  

sincerely yours,


*   For searches and help try:

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