Announcement

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

  • Inverse of -vec()- function?

    Hi everyone,

    I was wondering if there is a function in Mata that does the opposite of vec(), taking the number of columns as an argument. Consider the following example
    Code:
    : A = 1 , 4 \ 2 , 5 \ 3 , 6
    
    : A
           1   2
        +---------+
      1 |  1   4  |
      2 |  2   5  |
      3 |  3   6  |
        +---------+
    
    : vec(A)
           1
        +-----+
      1 |  1  |
      2 |  2  |
      3 |  3  |
      4 |  4  |
      5 |  5  |
      6 |  6  |
        +-----+
    
    : colshape(vec(A),2)
           1   2
        +---------+
      1 |  1   2  |
      2 |  3   4  |
      3 |  5   6  |
        +---------+
    
    : rowshape(vec(A),3)
           1   2
        +---------+
      1 |  1   2  |
      2 |  3   4  |
      3 |  5   6  |
        +---------+
    You can see that neither colshape() nor rowshape() return the original matrix. I have found the following workaround by transposing the vector into a row one and using rowshape:
    Code:
    : a = vec(A)'
    
    : rowshape(a,2)'
           1   2
        +---------+
      1 |  1   4  |
      2 |  2   5  |
      3 |  3   6  |
        +---------+
    But I was wondering if there was direct way. If not, this workaround could be the base for a built-in method invvec(), please?
    Alfonso Sanchez-Penalver

  • #2
    You can just directly transpose the rowshape or colshape results:
    Code:
    mata
        A = 1 , 4 \ 2 , 5 \ 3 , 6
        A
        B = vec(A)
        B
        rowshape(B,2)'
        colshape(B,3)'
    end

    Comment

    Working...
    X