Announcement

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

  • Is it possible to create Matrix of 1318 X 1737124 in mata

    I need to create N NxN matrices and than concatenate these matrices horizontally. My N is 1318 so concatenating these matrices will form a final matrix of 1318 X 1737124. Moreover, I need to create these matrices through loop. I am confused how should I code it. As i NxN matrix has -1 on its diagonal and 1 for each element of ith column except for (i,i) which is zero.Can you help regarding that how can I create N matrices in this manner. I am really new to stata.

  • #2
    You should first create the output matrix and then loop to create your 1318 matrices and place them in your output matrix by subscripting.

    Here is one solution

    Code:
    mata:
    
    k = 10  // start small
    a = J(k^2,k,.)
    I = -I(k)
    
    for (i=1 ; i<=k; i++) {
    
    // create the matrix N 
    N = I  
    N[,i] = J(k,1,1)
    N[i,i] = 0
    
    
    j = (i-1)*k+1
    
    a[|j,1\j+k-1,k|] = N
    
    }


    Comment


    • #3
      I need to horizontally concatenate the matrices, but the code is vertically concatenating. So I modified the code a bit but its giving error.
      code:
      mata
      k = 10
      a = J(k,k^2,.)
      I = -I(k)
      for (i=1 ; i<=k; i++) {
      N = I
      N[,i] = J(k,1,1)
      N[i,i] = 0
      j = (i-1)*k+1
      a[|j,1,j+k-1,k|] = N
      }
      Last edited by Midhat Khan; 25 Jun 2015, 00:36.

      Comment


      • #4
        Here is the modified code for horizontal concatenation and an example.
        Christophe

        Code:
        . mata:
        ------------------------------------------------- mata (type end to exit) -----------------------------------------
        : 
        : k = 4
        
        : a = J(k,k^2,.)
        
        : 
        : I = - I(k)
        
        : 
        : for (i=1;i<=k;i++) {
        > 
        > N = I 
        > N[,i] = J(k,1,1)
        > N[i,i] = 0 
        > 
        >  j= (i-1)*k+1
        > a[|1,j\k,j+k-1|] = N
        >                 
        > 
        > }
        
        : 
        : a
                1    2    3    4    5    6    7    8    9   10   11   12   13   14   15   16
            +---------------------------------------------------------------------------------+
          1 |   0    0    0    0   -1    1    0    0   -1    0    1    0   -1    0    0    1  |
          2 |   1   -1    0    0    0    0    0    0    0   -1    1    0    0   -1    0    1  |
          3 |   1    0   -1    0    0    1   -1    0    0    0    0    0    0    0   -1    1  |
          4 |   1    0    0   -1    0    1    0   -1    0    0    1   -1    0    0    0    0  |
            +---------------------------------------------------------------------------------+
        
        : 
        : 
        : end
        -------------------------------------------------------------------------------------------------------------------

        Comment


        • #5
          Thanks the code work fine, but I dont think mata will be able to do this for N=1318 as it goes beyond its limit.

          Comment


          • #6
            Actually I tried the code with N=1318 and it worked fine. It takes time to create the matrix in the the first place. In theory rows and columns of matrices in Mata cannot exceed 2^48-1. In practice it's the memory available on your computer and its operating system which will affect these limits. Mata's limits and memory utilization are explained here. It's true though that you will not be able to make a Stata matrix out of this Mata matrix, because it is too big. Finally, if you transpose this matrix you could be able to make a dataset out of it depending on whether you have a version of Stata which allow at least 1318 variables in your dataset (practically all versions of Stata except Small Stata).

            Comment

            Working...
            X