Announcement

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

  • Comparing different approaches passing matrix between Stata and Mata

    Hi,

    I am now coding programs using Stata and Mata simultaneously, so I have to pass information between Mata and Stata. I found out that there are more than one ways to pass things between these two environments and I am kind of curious which approach is more efficient.

    From Stata to Mata:
    Approach 1: Use st_matrix. And by using this approach, you need to pass the matrix's name to the Mata function. Currently, I am using this way.
    Approach 2: Pass a numeric matrix to the Mata function. I have tried this way, but it seems like it doesn't work well. Stata tells me she could not find the matrix. But in Stata manual, I have learned this approach. I don't know where went wrong. Followed is my code.

    capture program drop tt9
    program tt9,rclass
    mat A = (2,2,2,2,2)
    mata: test9(A)
    return mat m = r(m)

    end

    capture mata mata drop test9()
    mata:
    void test9(numeric matrix A)
    {
    numeric matrix B, C
    B = (2,3,4,5,6)
    C = A + B
    st_matrix("r(m)",C)
    }
    end

    From Mata to Stata:
    Approach 1:
    Use st_matrix. And by using this approach, you don't need to return anything in Mata function. Currently, I am now using this way.
    Approach 2: Use return in Mata function.

    I am curious which approach is better or they just work equally well. What's your opinions? Thank you very much for finishing reading these!

  • #2
    I have figured out one good reason why we want to use return in Mata function. When this Mata function is a sub-function, and its computational results would be used in another Mata function, then we should use return in Mata function.

    Comment


    • #3
      One more interesting finding, I think we can use type matrix as the argument of a Mata function only when this Mata function is called by another Mata function but not a Stata program. We can never directly pass a matrix from Stata to Mata. Am I correct about this?

      Comment


      • #4
        What do you mean you cannot pass a matrix from Stata to Mata? You can pass the name of the matrix and use st_matrix() in your mata function like your approach 1 suggests. For example, I can write

        Code:
        mata:
        
        real matrix  foo(string scalar StataMatrix)
        {
        return(st_matrix(StataMatrix))
        } end mat A = J(2,2,2) mata: A = foo("A") mata: A
        It's true that mata functions only recognize mata objects and in that way we cannot pass Stata objects to Mata functions.
        That being said in approach 2 you should modify your code as

        Code:
        mat A = (2,2,2,2,2)
        mata: test9(st_matrix("A"))
        return mat m = r(m)
        As you mention yourself the interest of passing a matrix to to your function is if you intend to use the function with other mata functions. And the only way to send a matrix from Stata to Mata or vice-versa is to use st_matrix().

        Comment


        • #5
          Thank you so much!!! I have been puzzled about this for a while!! I am much clearer about this now!!

          Comment

          Working...
          X