Announcement

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

  • Please tell me about the basics of Matrix.

    . mat A=1,2,3\4,5,6

    . mat list A

    A[2,3]
    c1 c2 c3
    r1 1 2 3
    r2 4 5 6

    . mat B=A[2,.]

    . mat list B

    symmetric B[1,1]
    c1
    r1 .

    . /* I expected the following results.
    > B[1,3]
    > c1 c2 c3
    > r1 4 5 6
    > */
    .
    . capture noisily mat C=A[|2,2\2,3|]
    A not found

    . capture noisily mat list C
    matrix C not found

    . * I don't understand the reason for the error.

  • #2
    The first error (unexpected results) is because you need more dots when specifying the range in Stata's matrix functions. So,use something like this.
    Code:
    mat B=A[2,....]
    The second error is because you've mixed up Stata's matrix syntax with Mata's. The use of vertical lines (pipes) is Mata's submatrix-extraction syntax. You can call Mata on-the-fly from Stata in order to make use of Mata's features, and so you can use, for example, this.
    Code:
    mata: st_matrix("C", st_matrix("A")[|2, 2 \ 2, 3|])
    .ÿ
    .ÿversionÿ17.0

    .ÿ
    .ÿclearÿ*

    .ÿ
    .ÿmatrixÿdefineÿAÿ=ÿ1,ÿ2,ÿ3ÿ\ÿ4,ÿ5,ÿ6

    .ÿ
    .ÿmatrixÿlistÿA

    A[2,3]
    ÿÿÿÿc1ÿÿc2ÿÿc3
    r1ÿÿÿ1ÿÿÿ2ÿÿÿ3
    r2ÿÿÿ4ÿÿÿ5ÿÿÿ6

    .ÿ
    .ÿmatrixÿdefineÿBÿ=ÿA[2,ÿ....]

    .ÿ
    .ÿmatrixÿlistÿB

    B[1,3]
    ÿÿÿÿc1ÿÿc2ÿÿc3
    r2ÿÿÿ4ÿÿÿ5ÿÿÿ6

    .ÿ
    .ÿmata:ÿst_matrix("C",ÿst_matrix("A")[|2,ÿ2ÿ\ÿ2,ÿ3|])

    .ÿ
    .ÿmatrixÿlistÿC

    C[1,2]
    ÿÿÿÿc1ÿÿc2
    r1ÿÿÿ5ÿÿÿ6

    .ÿ
    .ÿexit

    endÿofÿdo-file


    .

    Comment


    • #3
      Thank you for your prompt response!
      Matrix function is difficult and deep. I will study more.

      Comment

      Working...
      X