Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Matrix Multiplication row by row

    Hello- I am new to mata. I have a 236x3 dataset and I am trying to multiply each row by its transpose. This would give me a sequence of 236 3x3 matrices which I would like to add to get a final 3x3 matrix. Does anyone know how I would approach this? I would normally do this on R/Matlab but I can only access stata right now. I have attached a datex

    [CODE]
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input float(ret cgrowth beta)
    1.0745844 1.0099897 .99
    1.1065428 1.0173942 .99
    1.1028765 1.0130742 .99
    1.0041659 1.0254798 .99
    1.0546538 1.0193611 .99
    .9728066 1.0164609 .99
    1.0529184 1.0065401 .99
    .9240274 1.0108292 .99
    1.0285507 1.0180594 .99
    .9402671 .9996993 .99
    1.0857624 1.0060151 .99
    1.1196007 1.0014948 .99
    .9935445 1.0146269 .99
    1.032333 1.0085319 .99
    1.0722314 1.0212953 .99
    .9720475 1.0151385 .99
    .7872034 1.015757 .99
    1.0277625 1.0108033 .99
    1.121379 1.016991 .99
    1.0549921 1.0097009 .99
    1.0420611 1.0112089 .99
    1.0335879 1.0182106 .99
    1.046304 1.0121825 .99
    1.052786 1.0245839 .99
    1.0343125 1.019995 .99
    1.0304811 1.0218084 .99
    1.0067712 1.006235 .99
    1.0166372 1.0255004 .99
    .9763231 1.0160354 .99
    1.0694246 1.021043 .99
    1.0274566 1.031362 .99
    .9653792 1.0225891 .99
    .9496806 1.0108327 .99
    .9034694 1.0191216 .99
    1.0492425 1.0119587 .99
    1.1228681 1.0087613 .99
    1.004878 1.0183802 .99
    1.0669682 1.01428 .99
    .9975184 1.0146657 .99
    .9350057 1.0346887 .99
    1.1039912 1.0255167 .99
    1.0310303 1.0292408 .99
    1.0115906 1.0157049 .99
    .9773734 1.0211953 .99
    .9625652 1.0193944 .99
    .9530243 1.0171896 .99
    .9886168 1.0196882 .99
    .9736041 1.0178599 .99
    .8113355 1.0156498 .99
    1.1580032 1.018677 .99
    1.0942881 1.0102369 .99
    1.0885513 1.0287356 .99
    .9939188 1.0207292 .99
    .9863591 1.0178597 .99
    1.038133 1.0229235 .99
    1.0500538 1.023793 .99
    .9994403 1.0249966 .99
    1.0318276 1.024387 .99
    1.0678426 1.0319136 .99
    .9446844 1.0306771 .99
    .9348996 1.0187538 .99
    1.0399961 1.0219715 .99
    .8996588 1.017548 .99
    .9634034 1.0207858 .99
    .9150883 1.0318863 .99
    .7388372 1.0312263 .99
    1.0790052 1.0103039 .99
    1.2158693 1.027266 .99
    1.1419146 1.029075 .99
    .88108 1.0332743 .99
    1.0753547 1.0275344 .99
    1.1394832 1.0313399 .99
    1.014693 1.017711 .99
    1.0092059 1.0258834 .99
    1.0210947 1.0291053 .99
    .9158757 1.0297046 .99
    1.0209308 1.0226719 .99
    .9606887 1.0247914 .99
    .9851859 1.0296193 .99
    .9380652 1.0225167 .99
    1.070844 1.0423479 .99
    1.0733801 1.0218327 .99
    .9372928 1.0271056 .99
    1.0570179 1.0239666 .99
    1.0129935 1.0266272 .99
    1.0622874 1.0349023 .99
    .9873765 1.0269183 .99
    .9458032 1.0283217 .99
    1.1190127 1.0014064 .99
    1.0982143 1.0344081 .99
    1.0820979 1.0382984 .99
    1.0017679 1.0301297 .99
    .9647794 1.0165019 .99
    .8854508 1.0204484 .99
    1.0548288 1.0075974 .99
    .9135863 1.0193816 .99
    .9790104 1.0132545 .99
    1.0986223 1.0235658 .99
    1.1679124 1.0295328 .99
    1.0875996 1.0184575 .99
    end

  • #2
    Sun Yong Kim --

    I don't know if it helps, but awhile ago I wrote a package called rowmat_utils which you can read about here. The idea was to create a way of manipulating large amounts of small matrices in mata "in parallel."

    Matthew J. Baker


    Comment


    • #3
      i think this made-up example illustrates technique that accomplishes what you want within the context of Stata's matrix features - the result can be imported into Mata if you need it there, or you could probably translate this code into Mata if you want to work entirely in Mata.
      Code:
      cls
      * Example generated by -dataex-. To install: ssc install dataex
      clear
      input float(a b c)
        1   2   3
       10  20  30
      100 200 300
      end
      
      matrix total = J(3,3,0)
      forvalues o=1/`c(N)' {
          mkmat a b c in `o', matrix(row)
          matrix m = row'*row
          matrix list m
          matrix total = total + row'*row
      }
      matrix list total
      Code:
      . matrix total = J(3,3,0)
      
      . forvalues o=1/`c(N)' {
        2.         mkmat a b c in `o', matrix(row)
        3.         matrix m = row'*row
        4.         matrix list m
        5.         matrix total = total + row'*row
        6. }
      
      symmetric m[3,3]
         a  b  c
      a  1
      b  2  4
      c  3  6  9
      
      symmetric m[3,3]
           a    b    c
      a  100
      b  200  400
      c  300  600  900
      
      symmetric m[3,3]
             a      b      c
      a  10000
      b  20000  40000
      c  30000  60000  90000
      
      . matrix list total
      
      symmetric total[3,3]
             a      b      c
      a  10101
      b  20202  40404
      c  30303  60606  90909

      Comment

      Working...
      X