Title | Performing element-by-element operations on matrices | |
Author | Allen McDowell, StataCorp |
Matrix manipulations have been greatly enhanced since Stata 9 using the Mata language. One of the capabilities is an element-by-element calculation using the colon operator. For example:
. mata ------------------------------------------------- mata (type end to exit) ------ : x 1 2 +---------+ 1 | 1 2 | 2 | 3 4 | +---------+ : y 1 2 +---------+ 1 | 3 5 | 2 | 7 9 | +---------+ : z = x :/ y : z 1 2 +-----------------------------+ 1 | .3333333333 .4 | 2 | .4285714286 .4444444444 | +-----------------------------+ : end --------------------------------------------------------------------------------
Or, in Stata, suppose I have the following two matrices, A and B,
. matrix A=(1,2,3,4,5 \ 6,7,8,9,10) . mat list A A[2,5] c1 c2 c3 c4 c5 r1 1 2 3 4 5 r2 6 7 8 9 10 . matrix B=(11,12,13,14,15 \ 16,17,18,19,20) . matrix list B B[2,5] c1 c2 c3 c4 c5 r1 11 12 13 14 15 r2 16 17 18 19 20
I want to perform element-by-element division. That is, suppose I want to generate the matrix C so each element of C is the ratio of the corresponding elements in A and B, so that C[1,1] = A[1,1]/B[1,1]; C[1,2] = A[1,2]/B[1,2]; etc.
First, I generate the matrix C so that it has the correct dimensions. I must fill in the elements with some value, so I chose zeros.
. matrix C = J(2,5,0) . matrix list C C[2,5] c1 c2 c3 c4 c5 r1 0 0 0 0 0 r2 0 0 0 0 0
Now I can set up nested forvalues loops, with the outer loop indexing the rows and the inner loop indexing the columns.
forvalues i = 1/2 { forvalues j = 1/5 { matrix C[`i',`j']= A[`i',`j']/B[`i',`j'] } } matrix list C C[2,5] c1 c2 c3 c4 c5 r1 .09090909 .16666667 .23076923 .28571429 .33333333 r2 .375 .41176471 .44444444 .47368421 .5
I can easily modify the above code to accommodate other algebraic expressions involving the individual matrix elements and matrices of any dimensions.
Learn
Free webinars
NetCourses
Classroom and web training
Organizational training
Video tutorials
Third-party courses
Web resources
Teaching with Stata
© Copyright 1996–2024 StataCorp LLC. All rights reserved.
×
We use cookies to ensure that we give you the best experience on our website—to enhance site navigation, to analyze usage, and to assist in our marketing efforts. By continuing to use our site, you consent to the storing of cookies on your device and agree to delivery of content, including web fonts and JavaScript, from third party web services.
Cookie Settings
Last updated: 16 November 2022
StataCorp LLC (StataCorp) strives to provide our users with exceptional products and services. To do so, we must collect personal information from you. This information is necessary to conduct business with our existing and potential customers. We collect and use this information only where we may legally do so. This policy explains what personal information we collect, how we use it, and what rights you have to that information.
These cookies are essential for our website to function and do not store any personally identifiable information. These cookies cannot be disabled.
This website uses cookies to provide you with a better user experience. A cookie is a small piece of data our website stores on a site visitor's hard drive and accesses each time you visit so we can improve your access to our site, better understand how you use our site, and serve you content that may be of interest to you. For instance, we store a cookie when you log in to our shopping cart so that we can maintain your shopping cart should you not complete checkout. These cookies do not directly store your personal information, but they do support the ability to uniquely identify your internet browser and device.
Please note: Clearing your browser cookies at any time will undo preferences saved here. The option selected here will apply only to the device you are currently using.