Statalist


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

Re: st: re matrix substitution


From   Tirthankar Chakravarty <tirthankar.chakravarty@gmail.com>
To   statalist@hsphsun2.harvard.edu
Subject   Re: st: re matrix substitution
Date   Sat, 22 Aug 2009 16:33:15 +0100

While Scott's reply is valid, it will only take care of negative
values and not arbitrary thresholds. Here is a one-liner using the
-mm_cond()- function in Ben Jann's superb -moremata- package (SSC):
************************
mata
thresh = 3
A = (1, 2, 3\ 9, 8, 1)
mm_cond(A :>= J(rows(A), cols(A), thresh), A, J(rows(A), cols(A), 0))
end
************************

T

On Sat, Aug 22, 2009 at 4:13 PM, Scott Merryman<scott.merryman@gmail.com> wrote:
> It might be easier to do something like:
>
>  mata
>  A = 1,2,3 \ -1, -2, -3 \ 5,6,-9
>  A
>  C = (A + abs(A))/2
>  C
>  end
>
>
> Scott
>
>
> On Sat, Aug 22, 2009 at 4:52 AM, Marwan
> Elkhoury<elkhoury.marwan@gmail.com> wrote:
>> 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)
>>         end
>>
>>
>> capture mata mata drop mysubsi()
>> mata
>> real matrix mysubsi(real matrix A, real scalar thresh)
>> {
>>   real scalar r, c
>>   real matrix C
>>     r=rows(A)
>>     c=cols(A)
>>     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]
>> }
>>  }
>>  }
>>        return(C)
>>   }
>>
>> mata mosave mysubsi(), dir(c:\ado\personal\m) replace
>> end
>>
>> /// 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
>> appreciated.
>>
>> sincerely yours,
>>
>> Marwan
>
> *
> *   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/
>



-- 
To every ω-consistent recursive class κ of formulae there correspond
recursive class signs r, such that neither v Gen r nor Neg(v Gen r)
belongs to Flg(κ) (where v is the free variable of r).

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



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