Announcement

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

  • How to create matrix from a vector?

    Hi,

    given the following:

    . mat i = (1, 2, 3)

    . mat lis i

    i[1,3]
    c1 c2 c3
    r1 1 2 3

    .
    . mat N = i' * i

    . mat lis N

    symmetric N[3,3]
    c1 c2 c3
    c1 1
    c2 2 4
    c3 3 6 9

    ... hot to create matrix in which instead of products I would have differences:
    c1 c2 c3
    c1 0
    c2 1 0
    c3 2 1 0



  • #2
    Code:
    . matrix i = (1, 2, 3)
    
    . 
    . matrix P = i'*i
    
    . matrix list P, nohalf
    
    symmetric P[3,3]
        c1  c2  c3
    c1   1   2   3
    c2   2   4   6
    c3   3   6   9
    
    . 
    . scalar c = colsof(i)
    
    . matrix o = J(1,c,1)
    
    . matrix D1 = (o'*i)'
    
    . matrix D2 = (o'*i)
    
    . matrix D = (o'*i)' - (o'*i)
    
    . matrix list D1, nohalf
    
    D1[3,3]
        c1  c2  c3
    c1   1   1   1
    c2   2   2   2
    c3   3   3   3
    
    . matrix list D2, nohalf
    
    D2[3,3]
        c1  c2  c3
    c1   1   2   3
    c2   1   2   3
    c3   1   2   3
    
    . matrix list D, nohalf
    
    D[3,3]
        c1  c2  c3
    c1   0  -1  -2
    c2   1   0  -1
    c3   2   1   0
    
    .

    Comment


    • #3
      Thank you! I was suspecting there is no shortcut to do this

      Comment

      Working...
      X