*Tutorial 2 *Learning Mata by example **looping in Mata *--------------Start Example 1 ----------------* mata a=(1,2,3,4,5,6,7,8,9,10) a for (i=1;i<=10;i++) { a[i] //loops through a, one element of vector at a time } end *--------------End Example------------------* help m2_for *looping through rows and columns *--------------Start Example 2 ----------------* mata a=(1,2,3,4,5,6,7,8,9,10\11,12,13,14,15,16,17,18,19,100 ) a for (i1=1;i1<=cols(a);i1++) { //column loop, the max number is columns of a eg. cols(a) for (i=1;i<=rows(a);i++) { //row loop, the max number is rows of a eg. rows(a) a[i,i1] //loops through a, one element of vector at a time. Starting with first column } } end *--------------End Example------------------* help m2_for *--------------Start Example 3 ----------------* sysuse auto, clear set more off mata a=st_data(.,(2,3)) a for (i1=1;i1<=cols(a);i1++) { //column loop, the max number is columns of a eg. cols(a) for (i=1;i<=rows(a);i++) { //row loop, the max number is rows of a eg. rows(a) a[i,i1] //loops through a, one element of vector at a time. Starting with first column } } end *--------------End Example------------------* help mf_st_data *printing output rather than list matrix *--------------Start Example 4 ----------------* sysuse auto, clear set more off mata a=st_data(.,(2,3)) a for (i=1;i<=rows(a);i++) { //row loop, the max number is rows of a eg. rows(a) printf(" %13.2f", a[i,1]) printf(" %13.2f\n ", a[i, 2]) } end *--------------End Example------------------* help mf_st_data *looping through strings *--------------Start Example 5 ----------------* sysuse auto, clear set more off mata a=st_sdata(.,1) a for (i=1;i<=rows(a);i++) { printf("this is a make of car: %s\n", a[i]) //mata command for printing to results window } end *--------------End Example------------------* help mf_st_data //*************************************************************** *if statement //*************************************************************** *--------------Start Example 6 ----------------* mata a=(1,2,3,4,5,6,7,8,9,10) a for (i=1;i<=cols(a);i++) { if (a[i]<5) { a[i] //loops through a, one element of vector at a time } } end *--------------End Example------------------* help m2_if * Before putting mod() in an if statement, first see what mod() does *--------------Start Example 7 ----------------* mata a=(1,2,3,4,5,6,7,8,9,10) a for (i=1;i<=cols(a);i++) { b=mod(a[i],2) b } end *--------------End Example------------------* help mf_mod *list every 2nd element *--------------Start Example 8 ----------------* mata a=(1,2,3,4,5,6,7,8,9,10) a for (i=1;i<=cols(a);i++) { if (mod(i,2)==0) { a[i] //loops through a, one element of vector at a time } } end *--------------End Example------------------* *populate every a matrix with every 2nd element *--------------Start Example 9 ----------------* mata a=(1,2,3,4,5,6,7,8,9,10) a b=J(cols(a),1,.) b for (i=1;i<=cols(a);i++) { if (mod(i,2)==0) { b[i]=a[i] //loops through a, one element of vector at a time } } b end *--------------End Example------------------* *populate every a matrix with every 2nd element but with no missing values *--------------Start Example 10 ----------------* mata a=(1,2,3,4,5,6,7,8,9,10) a b=J(cols(a)/2,1,.) //note divide by 2 b z=1 //z is our new indexing scalar for (i=1;i<=cols(a);i++) { if (mod(i,2)==0) { b[z]=a[i] //loops through a, one element of vector at a time z=z+1 } } b end *--------------End Example------------------* **exercise **add up all the elements of a if they are less than 15 *--------------Start Example 11 ----------------* mata a=(1,2,13,41,5,6,17,8,19,110) a b=0 for (i=1;i<=cols(a);i++) { if (a[i]<15) { b=b+a[i] //loops through a, one element of vector at a time } } b end *--------------End Example------------------* **exercise **find the minimum in a *--------------Start Example 12 ----------------* mata a=(11,12,13,41,5,6,17,8,19,110) a b=a[1] for (i=1;i<=cols(a);i++) { if (a[i]0) A2 end *--------------End Example------------------* *End Tutorial 2