Notice: On March 31, it was **announced** that Statalist is moving from an email list to a **forum**. The old list will shut down on April 23, and its replacement, **statalist.org** is already up and running.

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

From |
"Nick Cox" <n.j.cox@durham.ac.uk> |

To |
<statalist@hsphsun2.harvard.edu> |

Subject |
st: RE: RE: Re: mata for-if-else |

Date |
Wed, 14 Apr 2010 14:44:23 +0100 |

Someone to whom Mata matters may want a hint on how Joseph's clever coding works. The matrix result of B :== 1 is determined elementwise, being 1 or 0 according to whether the corresponding element of B is 1. So, the expression yields an "indicator matrix" of 0s and 1s, which can be subtracted from A. The earlier solution using ?: hinged on the fact that official Mata does not have a elementwise ?: function for matrices. Ben Jann's -moremata- from SSC does have one called -mm_cond()-, but as it's a wrapper for a loop, there is no efficiency gain in using it. Nick n.j.cox@durham.ac.uk Abhimanyu Arora I see. Thanks very much Joseph and Nick for these useful tips and tricks (just beginning to get used to mata). Joseph Coveney Abhimanyu Arora wrote: I have simplified my problem to a large extent, but essentially has to do with using for-if-else commands in mata. I would like to create a matrix 'a' whose values depend on an existing matrix 'b' (both are 1000X1 vectors to be precise). I need to tell mata the dimension of a first. But all I get after executing the commands below is the original vector 'a'. What could be the possible error? I have tried adding and removing braces but it is not working... a=J(1000,1,.) for(i=1,i<1001,i++) { if (b[i]==1){ a[i]=2 } else {a[i]=3 } } a end ------------------------------------------------------------------------ ---- ---- Not to take away from any of the helpful replies that you've already received, but don't forget that Mata has colon operators that help avoid loops when dealing with vectors and matrices. For example, your simplified example can be handled succinctly without the need for any explicit looping. : B = (. \ 1 \ 2 \ 3) : : A = J(rows(B), 1, 3) - (B :== 1) : : A 1 +-----+ 1 | 3 | 2 | 2 | 3 | 3 | 4 | 3 | +-----+ It could be more efficient, too, than an elaborated for-loop with an imbedded if-then-else section. You'll need to consider the trade-offs in code readability (maintainability) if you get too carried away with one-liners like that. I'm sure that your real problem is more complicated than the simplified version you posted for illustration. And it might not appear, at least at first glance, to readily lend itself to a colon operator. But keep in mind that you've got options (operators, matrix functions) available in Mata to help in just these kinds of situations. It's a domain-specific programming language that's designed to be rich in operations that matter in statistical computation. * * 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/

**References**:**st: mata for-if-else***From:*"Abhimanyu Arora" <abhimanyu.arora@student.kuleuven.be>

**st: Re: mata for-if-else***From:*"Joseph Coveney" <jcoveney@bigplanet.com>

**st: RE: Re: mata for-if-else***From:*"Abhimanyu Arora" <abhimanyu.arora@student.kuleuven.be>

- Prev by Date:
**Re: st: RE: Newton-Raphson (not concave)** - Next by Date:
**RE: st: RE: RE: graphing question - how to smooth lines connecting observations** - Previous by thread:
**st: RE: Re: mata for-if-else** - Next by thread:
**Re: st: mata for-if-else** - Index(es):