Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
re:st: Mata: editing values in a matrix when a condition is met
From
Christopher Baum <[email protected]>
To
"[email protected]" <[email protected]>
Subject
re:st: Mata: editing values in a matrix when a condition is met
Date
Tue, 7 Feb 2012 23:08:10 -0500
<.>
Pablo said
I need to edit the values of a large matrix in Mata. More precisely, I
need to replace all values smaller than a number X by X, and all numbers
greater than Y by Y. The functions editvalue and _editvalue allow you to
replace a scalar by other in a matrix, but not to specify a condition
like x < X to trigger the replacement. Is there a way of doing this
economically, that is, without looping over all elements of the matrix?
Using Ben Jann's -moremata- from SSC to do the winsorizing:
: a=runiform(5,5)
: a
1 2 3 4 5
+-----------------------------------------------------------------------+
1 | .3590218453 .0936944787 .3864043248 .8279724028 .7848427724 |
2 | .9620482328 .7566794683 .583137149 .4482575031 .2592765098 |
3 | .3781643927 .4720275472 .9826083879 .3655504752 .9070760873 |
4 | .9481407967 .8307412954 .5391620845 .0043095979 .379461355 |
5 | .686426234 .1595839853 .8743242584 .7100557149 .7607446529 |
+-----------------------------------------------------------------------+
: b = mm_cond(a :>= 0.8, 0.8, a)
: b = mm_cond(b :<=0.2, 0.2, b)
: b
1 2 3 4 5
+-----------------------------------------------------------------------+
1 | .3590218453 .2 .3864043248 .8 .7848427724 |
2 | .8 .7566794683 .583137149 .4482575031 .2592765098 |
3 | .3781643927 .4720275472 .8 .3655504752 .8 |
4 | .8 .8 .5391620845 .2 .379461355 |
5 | .686426234 .2 .8 .7100557149 .7607446529 |
+-----------------------------------------------------------------------+
*
* 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/