Announcement

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

  • help on stacking matrices

    Hi, Stata gurus,

    this seems to be easy, once with right Mata commands.

    I have N matrices, same number of columns, but different number of rows. They are named matrix1 .. matrixN.

    I would like to stack them all, dropping the original ones, using a loop.

    Any clue ? thanks, in advance.

  • #2
    Stacking matrices (concatenation) is easy enough as long as the column dimension is the same for all matrices. This is done using the '\' operator.

    I would like to stack them all, dropping the original ones, using a loop.
    This is unclear to me. I interpret this to mean that you want to drop the original (component) matrices once they have been stacked into a single matrix.

    [code]
    mata:
    A = I(2)
    B = I(2) :* 2
    C = (1,2\3,4\5,6)
    D1 = A \ B \ C
    D1

    // put component matrix names into a vector
    matnames = "A", "B", "C"

    // make two new strings, one for the martrix joining operation and the other a simple list for dropping them
    // push results to local macros
    matop = matnames[1]
    mattodrop = matnames[1]
    for (i = 2; i <= length(matnames); i++) {
    matop = sprintf("%s \ %s", matop, matnames[i])
    mattodrop = sprintf("%s %s", mattodrop, matnames[i])
    }
    st_local("matop", matop)
    st_local("mattodrop", mattodrop)

    D2 = `matop'
    D2
    D1 == D2

    // drop component matrices
    mata drop `mattodrop'

    // drop local macros
    st_local("matop", "")
    st_local("mattodrop", "")
    end
    [code]

    Selected results

    Code:
    : D1
           1   2
        +---------+
      1 |  1   0  |
      2 |  0   1  |
      3 |  2   0  |
      4 |  0   2  |
      5 |  1   2  |
      6 |  3   4  |
      7 |  5   6  |
        +---------+
    
    : D2
           1   2
        +---------+
      1 |  1   0  |
      2 |  0   1  |
      3 |  2   0  |
      4 |  0   2  |
      5 |  1   2  |
      6 |  3   4  |
      7 |  5   6  |
        +---------+

    Comment


    • #3
      Leornando, thanks for your quickly replay. Actually, the challenge here is, also, the fact matrices are named matrix1 .. matrixN.

      I did some search on the forum and found how to do the trick:



      Code:
      : mata clear
      
      : A1 = I(2)
      : A2 = I(2)
      : A3 = I(2)
      : A4 = I(2)
      
      : mata describe A*
      
            # bytes   type                        name and extent
      -------------------------------------------------------------------------------
                 32   real matrix                 A1[2,2]
                 32   real matrix                 A2[2,2]
                 32   real matrix                 A3[2,2]
                 32   real matrix                 A4[2,2]
      -------------------------------------------------------------------------------
      
      : AN=J(0,2,.)
      
      : for (i=1;i<=4;i++) {
      > stata("mata: AN=AN\"+"A"+strofreal(i))
      > stata("mata: mata drop "+"A"+strofreal(i))
      > }
      
      : mata describe A*
      
            # bytes   type                        name and extent
      -------------------------------------------------------------------------------
                128   real matrix                 AN[8,2]
      -------------------------------------------------------------------------------
      thks again,

      Comment


      • #4
        Using Stata locals can streamline things a bit:
        Code:
        forval j=1/4 {
         mata A`j'=I(2)
        }
        mata AN=J(0,2,.)
        
        forval j=1/4 {
         mata AN=AN\A`j'
         mata mata drop A`j'
        }
        
        mata AN

        Comment

        Working...
        X