Bookmark and Share

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   Nick Cox <[email protected]>
To   [email protected]
Subject   Re: st: Mata: editing values in a matrix when a condition is met
Date   Wed, 8 Feb 2012 09:24:12 +0000

This solution is convenient but there are still loops, within -mm_cond()-.

There is a one-line alternative with no extra functions:

: a = runiform(5,5)

: a
                 1             2             3             4             5
    +-----------------------------------------------------------------------+
  1 |  .1369840784    .643220668   .5578016951   .6047949435    .684175977  |
  2 |  .1086679425   .6184581316   .0610637818   .5552388297   .8714490768  |
  3 |  .2551498795   .0445188014   .4241557193   .8983461575   .5219247625  |
  4 |  .8414094402   .2110076593   .5644091703   .2648020918   .9477426449  |
  5 |  .2769154108   .1180158504   .4079702524   .7219491643   .8716910495  |
    +-----------------------------------------------------------------------+

: b = 0.8 *  (a :>= 0.8) + 0.2 * (a :<= 0.2) + (a :> 0.2 :& a:< 0.8) :* a

: b
                 1             2             3             4             5
    +-----------------------------------------------------------------------+
  1 |           .2    .643220668   .5578016951   .6047949435    .684175977  |
  2 |           .2   .6184581316            .2   .5552388297            .8  |
  3 |  .2551498795            .2   .4241557193            .8   .5219247625  |
  4 |           .8   .2110076593   .5644091703   .2648020918            .8  |
  5 |  .2769154108            .2   .4079702524   .7219491643            .8  |
    +-----------------------------------------------------------------------+

Nick

On Wed, Feb 8, 2012 at 4:08 AM, Christopher Baum <[email protected]> wrote:

> 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  |
>    +-----------------------------------------------------------------------+


Pablo Mitnik

> 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?

*
*   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–2018 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   Site index