⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 m1_first.hlp

📁 是一个经济学管理应用软件 很难找的 但是经济学学生又必须用到
💻 HLP
📖 第 1 页 / 共 3 页
字号:
{smcl}
{* 04may2005}{...}
{cmd:help m1 first}
{hline}

{title:Title}

{pstd}
{bf:[M-1] first -- Introduction and first session}


{title:Description}

{pstd}
Mata is a component of Stata.  It is a matrix programming language which 
can be used interactively or as an extension for do-files and ado-files.
Thus

{phang2}
  1.  Mata can be used by users who want to think in matrix terms and
      perform (not necessarily simple) matrix calculations interactively, and

{phang2}
  2.  Mata can be used by advanced Stata programmers who want to add features
      to Stata.

{pstd}
Mata has something for everybody.  

{pstd}
Primary features of Mata are that it is fast and that it is C-like.


{title:Remarks}

{pstd}
This introduction is presented under the headings

	{bf:Invoking Mata}
	{bf:Using Mata}
	{bf:Making mistakes:  interpreting error messages}
	{bf:Working with real numbers, complex numbers, and strings}
	{bf:Working with scalars, vectors, and matrices}
	{bf:Working with functions}
	{bf:Distinguishing real and complex values}
	{bf:Working with matrix and scalar functions}
	{bf:Performing element-by-element calculations:  colon operators}
	{bf:Writing programs}
	{bf:More functions}
	{bf:Mata environment commands}
	{bf:Exiting Mata}
	

{title:Invoking Mata}

{pstd}
To enter Mata, type {cmd:mata} at Stata's dot prompt and press enter; to exit
Mata, type {cmd:end} at Mata's colon prompt:

	. {cmd:mata}{col 48}<- type {cmd:mata} to enter Mata
	{hline 10} mata (type {cmd:end} to exit} {hline 3}
        : {cmd:2+2}{col 48}<- type Mata statements at the
          {res:4}{col 48}   colon prompt

	: {cmd:end}{col 48}<- type {cmd:end} to return to Stata
	{hline 38}

	. _{col 48}<- you are back to Stata


{title:Using Mata}

{pstd}
When you type a statement into Mata, Mata compiles what you typed and, if 
it compiled without error, executes it:

{pstd}

	: {cmd:2+2}
	  4

	: _

{pstd}
We typed {cmd:2+2}, a particular example from the general class of
expressions.  Mata responded with 4, the evaluation of the expression.

{pstd}
Often what you type are expressions, although you will probably choose
more complicated examples.  When an expression is not assigned to a variable,
the result of the expression is displayed.  Assignment is performed by the
{cmd:=} operator:

	: {cmd:x = 2 + 2}

	: {cmd:x}
	  4

	: _

{pstd}
When we type "{cmd:x=2+2}", the expression is evaluated and stored in the
variable we just named {cmd:x}.  The result is not displayed.  We can look at
the contents of {cmd:x}, however, simply by typing "{cmd:x}".  From Mata's
perspective, {cmd:x} is not only a variable, it is also an expression, albeit
a rather simple one.  Just as {cmd:2+2} says to load 2, load another 2, and
add them, the expression {cmd:x} says to load {cmd:x} and stop there.

{pstd}
As an aside, Mata distinguishes uppercase and lowercase.  {cmd:X} is not
the same as {cmd:x}:

	: {cmd:X = 2 + 3}

	: {cmd:x}
	  4

	: {cmd:X}
	  5


{title:Making mistakes:  interpreting error messages}

{pstd}
If you make a mistake, Mata complains, and then you continue on your way.
For instance, 

	: {cmd:2,,3}
	{err:invalid expression}
	r(3000);

	: _

{pstd}
"{cmd:2,,3}" makes no sense to Mata, so Mata complained.  This is an example 
of what is called a compile-time error; Mata could not make sense out
of what we typed.

{pstd}
The other kind of error is called a run-time error.  For example, we have no
variable called {cmd:y}.  Let us ask Mata to show us the contents of {cmd:y}:

	: {cmd:y}
	         {err:<istmt>:   3499  y not found}
	r(3499);

	: _

{pstd}
In this case, what we typed made perfect sense -- show me {cmd:y} -- but
{cmd:y} has never been defined.
This ugly message is called a run-time error message -- see 
{bf:{help m2_errors:[M-2] errors}}
for a complete description -- but all that's 
important is to understand the difference between

	{err:invalid expression}

{pstd}
    and

	{err:<istmt>:   3499  y not found}

{pstd}
The run-time message is prefixed by an identity ({cmd:<istmt>} in this case)
and a number (3499 in this case).  Mata is telling us, "I was executing
your {it:istmt} [that's what everything you type is called] and I got error
3499, the details of which are that I was unable to find {cmd:y}."

{pstd}
The compile-time error message is of a simpler form:  "invalid expression".
When you get such unprefixed error messages, that means Mata could not 
understand what you typed.  When you get the more complicated error 
message, that means Mata understood what you typed, but there was a problem 
performing your request.

{pstd}
Another way to tell the difference between compile-time errors 
and run-time errors is to look at the return code.  Compile-time errors have a
return code of 3000:

	: {cmd:2,,3}
	{err:invalid expression}
	r(3000);

{pstd}
Run-time errors have a return code that might be in the 3000s, but is never 
3000 exactly:

	: {cmd:y}
	         {err:<istmt>:   3499  y not found}
	r(3499);


{pstd}
Whether the error is compile-time or run-time, once the error message is
issued, Mata is ready to continue just as if the error never happened.


{title:Working with real numbers, complex numbers, and strings}

{pstd}
As we have seen, Mata works with real numbers:

	: {cmd:2+3}
	  5

{pstd}
Mata also understands complex numbers; you write the imaginary part 
by suffixing a lowercase {cmd:i}:

	: {cmd:1+2i + 4-1i}
	  5+1i

{pstd}
For imaginary numbers, you can omit the real part:

	: {cmd:1+2i - 2i}
	  1

{pstd}
Whether a number is real or complex, you can use the same computer 
notation for the imaginary part as you would for the real part:

	: {cmd:2.5e+3i}
	  2500i

	: {cmd:1.25e+2+2.5e+3i}              /* i.e., 1.25e+02 + 2.5e+03i */
	 125 + 2500i

{pstd}
We purposely wrote the last example in nearly unreadable form just to 
emphasize that Mata could interpret it.  


{pstd}
Mata also understands strings, which you write enclosed in double quotes:

	: {cmd:"Alpha"+"Beta"}
	  AlphaBeta

{pstd}
Just like Stata, Mata understands simple and compound double quotes:

	: {cmd:`"Alpha"'+`"Beta"'}
	  AlphaBeta

{pstd}
You can add complex and reals,

	: {cmd:1+2i + 3}
	  4+2i

{pstd}
but you may not add reals or complex to strings:

	: {cmd:2 + "alpha"}
	         {err:<istmt>:   3250  type mismatch}
	r(3250);

{pstd}
We got a run-time error.  Mata understood {cmd:2 + "alpha"}
alright, it just could not perform our request. 


{title:Working with scalars, vectors, and matrices}

{pstd}
In addition to understanding scalars -- be they real, complex, or string --
Mata understands vectors and matrices of real, complex, and string elements:

	: {cmd:x = (1,2)}

	: {cmd:x}
        {res}       {txt}1   2
            {c TLC}{hline 9}{c TRC}
          1 {c |}  {res}1   2{txt}  {c |}
            {c BLC}{hline 9}{c BRC}{txt}

{pstd}
{cmd:x} now contains the row vector (1,2).  We can add vectors:

	: {cmd:x + (3,4)}
        {res}       {txt}1   2
            {c TLC}{hline 9}{c TRC}
          1 {c |}  {res}4   6{txt}  {c |}
            {c BLC}{hline 9}{c BRC}{txt}

{pstd}
The "{cmd:,}" is the column-join operator; things like {cmd:(1,2)} are
expressions, just as {cmd:(1+2)} is an expression:

	: {cmd:y = (3,4)}

	: {cmd:z = (x,y)}

	: {cmd:z}
        {res}       {txt}1   2   3   4
            {c TLC}{hline 17}{c TRC}
          1 {c |}  {res}1   2   3   4{txt}  {c |}
            {c BLC}{hline 17}{c BRC}{txt}

{pstd}
In the above, we could have dispensed with the parentheses and typed
"{cmd:y=3,4}" followed by "{cmd:z=x,y}", just as we could using the {cmd:+}
operator, although most people find vectors more readable when enclosed in 
parentheses.

{pstd}
"{cmd:\}" is the row-join operator:

	: {cmd:a = (1\2)}

	: {cmd:a}
            {c TLC}{hline 5}{c TRC}
          1 {c |}  {res}1{txt}  {c |}
          2 {c |}  {res}2{txt}  {c |}
            {c BLC}{hline 5}{c BRC}{txt}

	: {cmd:b = (3\4)}

	: {cmd:c = (a\b)}

	: {cmd:c}
        {res}       {txt}1
            {c TLC}{hline 5}{c TRC}
          1 {c |}  {res}1{txt}  {c |}
          2 {c |}  {res}2{txt}  {c |}
          3 {c |}  {res}3{txt}  {c |}
          4 {c |}  {res}4{txt}  {c |}
            {c BLC}{hline 5}{c BRC}

{pstd}
Using the column-join and row-join operators, we can enter matrices:

	: {cmd:A = (1,2 \ 3,4)}

	: {cmd:A}
        {res}       {txt}1   2
            {c TLC}{hline 9}{c TRC}
          1 {c |}  {res}1   2{txt}  {c |}
          2 {c |}  {res}3   4{txt}  {c |}
            {c BLC}{hline 9}{c BRC}{txt}

{pstd}
The use of these operators is not limited to scalars.  Remember,
{cmd:x} is the row vector (1,2), {cmd:y} is the row vector (3,4), {cmd:a} is
the column vector (1\2), and {cmd:b} is the column vector (3\4).  Therefore,

	: {cmd:x\y}
        {res}       {txt}1   2
            {c TLC}{hline 9}{c TRC}
          1 {c |}  {res}1   2{txt}  {c |}
          2 {c |}  {res}3   4{txt}  {c |}
            {c BLC}{hline 9}{c BRC}{txt}

	: {cmd:a,b}
            {c TLC}{hline 9}{c TRC}
          1 {c |}  {res}1   3{txt}  {c |}
          2 {c |}  {res}2   4{txt}  {c |}
            {c BLC}{hline 9}{c BRC}{txt}

{pstd}
But if we try something nonsensical, we get an error:

	: {cmd:a,x}
	         {err:<istmt>:  3200  nonconformable matrices}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -