Statalist The Stata Listserver


[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

RE: st: mata function : coding optional argument


From   "U.A.QU.AP" <[email protected]>
To   [email protected]
Subject   RE: st: mata function : coding optional argument
Date   Tue, 18 Apr 2006 23:21:14 +0100 (CET)

thanks to Ben Jann. You give an elegant solution. 


---- Original message ---- 
>Date: Tue, 18 Apr 2006 22:33:08 +0200
>From: "Ben  Jann" <[email protected]>  
>Subject: RE: st: mata function : coding optional argument  
>To: <[email protected]>
>
>Depending on the context, you could code something like
>
> transmorphic matrix chak(
>  real colvector dim1,
>  real colvector dim2,
>  | real colvector pop , 
>    real scalar b)
> {
>	pointer scalar ppop
>
>      if (args()<3 | pop==.) ppop = &J(rows(dim1),1,1)
>      else ppop = &pop
> 
>      ... code using *ppop instead of pop ...
> }
>
>This allows the user to type
>
>  : chak(dim1, dim2, ., b)
>
>This solution however assumes that pop=. is not a 
>meaningful input to chak().
>ben
>
>> -----Original Message-----
>> From: [email protected] [mailto:owner-
>> [email protected]] On Behalf Of U.A.QU.AP
>> Sent: Tuesday, April 18, 2006 9:34 PM
>> To: [email protected]
>> Subject: Re: st: mata function : coding optional argument
>> 
>> That is, there is no disponible solution of such problem.
>> AbdelRahmen
>> 
>> ---- Original message ----
>> >Date: Tue, 18 Apr 2006 08:10:20 -0500
>> >From: [email protected] (William Gould, Stata)
>> >Subject: Re: st: mata function : coding optional argument
>> >To: [email protected]
>> >
>> >AbdelRahmen El Lahga <[email protected]> writes,
>> >
>> >> I'm writing a mata function say:
>> >>
>> >>       transmorphic matrix chak(real colvector dim1,
>> >>                                real colvector dim2,
>> >>                                | real colvector pop , real
>> scalar b)
>> >>       {......}
>> >>
>> >>
>> >> in which "pop" and the scalar "b" are optional arguments as
>> >> you can see above
>> >>
>> >> My question is how to code the optional arguments. I
want that
>> >> if user dont specifies  the vector "pop"  mata must
replace it
>> >> by a vector of ones  and similary for b.
>> >> Following the help my solution was
>> >>
>> >>       nb_arg=args()
>> >>       if (nb_arg<3) pop=J(rows(dim1),1,1)
>> >>       if (nb_arg<4) b=1
>> >>
>> >> but this solution don't allow  the user to specifies the
>> >> scalar b and omit the 3rd argument pop
>> >
>> >What AbdelRahmen has coded is exactly right, and Abdel is
>> also right
>> >that user can omit the third and fourth arguments, or the
>> fourth, but
>> >not just the third.  Mata's arguments are positional, and
>> positional
>> >means that if you specify 3 argments, they are the first,
>> second, and
>> >third, not the first, second, and fourth.
>> >
>> >-- Bill
>> >[email protected]
>> >*
>> >*   For searches and help try:
>> >*   http://www.stata.com/support/faqs/res/findit.html
>> >*   http://www.stata.com/support/statalist/faq
>> >*   http://www.ats.ucla.edu/stat/stata/
>> *
>> *   For searches and help try:
>> *   http://www.stata.com/support/faqs/res/findit.html
>> *   http://www.stata.com/support/statalist/faq
>> *   http://www.ats.ucla.edu/stat/stata/
>
>*
>*   For searches and help try:
>*   http://www.stata.com/support/faqs/res/findit.html
>*   http://www.stata.com/support/statalist/faq
>*   http://www.ats.ucla.edu/stat/stata/
*
*   For searches and help try:
*   http://www.stata.com/support/faqs/res/findit.html
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/



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