*Tutorial 6 *Learning Mata by example *working with a pointer *--------------Start Example 1 ----------------* clear mata //clear matrices from memory mata a=(1,1\1,4\1,9) a a1=(3,3\3,3\3,3) a1 b=J(4,1,NULL) b b[1]=&a b *b[1] b[2]=&a1 b *b[2] c=(*b[2])[2,2] c end *--------------End Example------------------* help m2_pointers *working with a pointer *--------------Start Example 2 ----------------* clear sysuse auto set more off mata p = J(10, 1, NULL) //setting up pointer vector p for (i=1;i<=10;i++) { g=st_data(1::5, i+1..i+2) g p[i] = &J(rows(g),cols(g),.) for (zcol=1;zcol<=cols(g);zcol++) { for (zrow=1;zrow<=rows(g);zrow++) { (*p[i])[zrow,zcol] = g[zrow,zcol ] } } } //end get matrix loop p *p[1] *p[2] v=(*p[1],*p[2]) v end *--------------End Example------------------* *working with a optimize *--------------Start Example 3 ----------------* set more off sysuse auto, clear mata a=st_data(.,"mpg") a b=(a,a)' b end clear mata mata void myeval(todo, x, y, g, H) { y = (-x^5 +x^3+x^2+x - 3) } S = optimize_init() optimize_init_evaluator(S, &myeval()) optimize_init_params(S, 0) x1 = optimize(S) x1 st_local("optline", strofreal(x1)) end display "`optline'" twoway function y = (-x^5 +x^3+x^2+x - 3), xline(`optline')range(0 2) *--------------End Example------------------* *end tutorial 6 exit