Announcement

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

  • colon operator :* not working

    Hi All,

    I am writing an ado that also involves a simple mata function. The mata calls the stata matrices correctly but returns an error when I try to generate a new matrix as an element-wise multiplication of two matrices.

    For reference

    mata :
    real matrix est_costx(string x1,, string b, string A1)
    {
    x1 = st_matrix("x1")
    b = st_matrix("b")
    A1 = st_matrix("A1")
    a = A1 :* x1
    }
    end

    where x1 b and A1 are column vectors
    Thanks

  • #2
    Originally posted by Jay Dev View Post
    The mata calls the stata matrices correctly . . .
    The code you show won't even compile because of the syntax error in the argument list. Beyond that, you're trying to assign real matrices to string variables.

    Try something like the following instead.
    Code:
    version 18.0
    
    clear *
    
    mata:
    mata set matastrict on
    
    real matrix function est_costx(string scalar x1, string scalar b, string scalar A1) {
        real matrix X, Y, Z
        X = st_matrix(x1)
        Y = st_matrix(b); pragma unused Y
        Z = st_matrix(A1)
        return(X :* Z)
    }
    end
    
    matrix input A = (1 \ 2 \ 3)
    matrix input B = (4 \ 5 \ 6)
    matrix input C = (7 \ 8 \ 9)
    
    mata: est_costx("A", "B", "C")
    
    exit

    Comment

    Working...
    X