*Tutorial 3 *Learning Mata by example *getting a subset of a matrix *--------------Start Example 1 ----------------* mata a=(1,2,3\4,5,6\7,8,9) a b=a[2::rows(a), 2..cols(a)] //note. :: for rows and .. cols b end *--------------End Example------------------* help m5_intro // for help on rows() and cols() *getting a subset of a matrix *--------------Start Example 2 ----------------* mata a=(1,2,3\4,5,6\7,8,9) a b=a[(1::rows(a)-1), 2..cols(a)] //note. :: for rows and .. cols b end *--------------End Example------------------* help m5_intro // for help on rows() and cols() *getting a subset of the matrix *--------------Start Example 3 ----------------* mata a=(1,2,3\4,5,6\7,8,9) a b=a[(1+1)/2::rows(a), 2..cols(a)] b end *--------------End Example------------------* *getting a subset of the matrix *--------------Start Example 4 ----------------* mata a=(1,2,3\4,5,6\7,8,9) a b=a[(1\3\2), (1,3)] b end *--------------End Example------------------* *getting a subset of the matrix *--------------Start Example 5 ----------------* mata a=(1,2,3,4,5,6,7\8,9,10,11,12,13,14\15,16,17,18,19,20,21\22,23,24,25,26,27,28\29,30,31,32,33,34,35\36,37,38,39,40,41,42\43,44,45,46,47,48,49) a b=a[|2,3 \ 4,7|] b end *--------------End Example------------------* help m2_subscripts **op_conditional -- Conditional operator *--------------Start Example 6 ----------------* mata a=1 a k=2 k n=2 n b=(a==1 ? n-1 : n-k) //statement true then b=n-1 b b=(a==2 ? n-1 : n-k) //statement false then b=n-k b end *--------------End Example------------------* help m2_op_conditional *Colon operators *appling somehting to every element *--------------Start Example 7 ----------------* mata a=(1\2\3\4\5\6\7) a b=a :== 4 b c=a :* b c d=b :* 10 d end *--------------End Example------------------* help m2_op_colon *exercise: if aa equals 4 then replace with 100 *--------------Start Example 8 ----------------* mata aa=(1\2\3\4\5\6\7) aa for (i=1; i<=rows(a);i++) { if (aa[i]==4) { aa[i]=100 } } aa end *--------------End Example------------------* *exercise: if aa equals 4 then replace with 100 *--------------Start Example 9 ----------------* mata aa=(1\2\3\4\5\6\7) aa b= aa :==4 b c=b :* 100 c d= c :==0 d e=d :+c e f=a :*e f end *--------------End Example------------------* **End of tutorial 3