Announcement

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

  • Converting matrix into a stacked column vector

    I have data that looks like the following:


    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input float(var1 var2 var3)
    1 2 3
    4 5 6
    7 8 9
    end

    I want to vectorize this matrix into a column vector which would look like:

    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input float var1
    1
    2
    3
    4
    5
    6
    7
    8
    9
    end
    I am not quite sure how to do this in Stata? Any guidance is much appreciated.





  • #2
    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input float(var1 var2 var3)
    1 2 3
    4 5 6
    7 8 9
    end
    
    
    gen long obs_no = _n
    reshape long var, i(obs_no) j(_j)
    sort obs_no _j
    And if you have no further need of them, just drop obs_no and _j.

    Comment


    • #3
      Another way to do it:

      Code:
      xpose, clear 
      stack v1 v2 v3, into(var) clear

      Comment


      • #4
        Hi!

        i have a question:

        How to convert an NxN matrix into a single stacked vector?

        if i have the matrix "A" (3x3),
        and i want it to be in a column vector (9x1)
        some command for this? thanks

        Comment


        • #5
          I would work in Mata.


          Code:
          . mat whatever = (1,4,7\2, 5, 8\3, 6, 9)
          
          . mat li whatever
          
          whatever[3,3]
              c1  c2  c3
          r1   1   4   7
          r2   2   5   8
          r3   3   6   9
          
          : colshape(st_matrix("whatever"), 1)
                 1
              +-----+
            1 |  1  |
            2 |  4  |
            3 |  7  |
            4 |  2  |
            5 |  5  |
            6 |  8  |
            7 |  3  |
            8 |  6  |
            9 |  9  |
              +-----+
          
          : rowshape(st_matrix("whatever"), 9)
                 1
              +-----+
            1 |  1  |
            2 |  4  |
            3 |  7  |
            4 |  2  |
            5 |  5  |
            6 |  8  |
            7 |  3  |
            8 |  6  |
            9 |  9  |
              +-----+
          
          : rowshape(st_matrix("whatever")', 9)
                 1
              +-----+
            1 |  1  |
            2 |  2  |
            3 |  3  |
            4 |  4  |
            5 |  5  |
            6 |  6  |
            7 |  7  |
            8 |  8  |
            9 |  9  |
              +-----+
          
          : colshape(st_matrix("whatever")', 1)
                 1
              +-----+
            1 |  1  |
            2 |  2  |
            3 |  3  |
            4 |  4  |
            5 |  5  |
            6 |  6  |
            7 |  7  |
            8 |  8  |
            9 |  9  |
              +-----+
          There are historic add-ons, but work in Mata, even if you want the final result to be a Stata matrix.


          STB-56 dm79 . . . . . . . . . . . . . . . . . . Yet more new matrix commands
          (help matcorr, matewmf, matvsort, svmat2 if installed) . . N. J. Cox
          7/00 pp.4--8; STB Reprints Vol 10, pp.17--23
          commands to produce a correlation matrix, elementwise monadic
          function of another matrix, selected subsets of matrix rows
          and columns, vec or vech of a matrix, elements sorted within
          a vector, matrix from a vector, and commands to save matrices
          see mata matrix language incorporated into Stata 9.0

          Comment


          • #6
            Hello, who's here in 2024?

            If you are OK with matrices, there is yet another way to do this, at least in STATA 18:
            Code:
            matrix whatever = (1,4,7\2, 5, 8\3, 6, 9)
            matrix list whatever
            matrix stacked_transposed_whatever=vec(whatever')
            matrix list stacked_transposed_whatever
            If, as in the first post, you prefer working with variables, then the solution in #3 seems most efficient.

            Comment

            Working...
            X