Announcement

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

  • From Mata scalar to Stata local

    Dear Statalisters,

    I'd like to extract the maximum value of a Matrix (defined in mata) and use it in a Stata if function.
    Look at what I've done so far using resindex and st_store commands
    Code:
    quietly {
    . mata : A=(0,1\2,0)
    
    . mata : A
    
    mata: MaxA=max(A)
    
    mata: st_matrix("MaxA", MaxA)
    capture drop maxVal
    mata: resindex = st_addvar("float","maxVal")
    mata: st_store((1,rows(MaxA)),resindex,MaxA)
    noi su maxVal
    
    if maxVal[1]>1 {
     noi display "the network is weigthed"
    }
    else{
     noi display "the network is not weigthed"
    }
    }
    Stata returns :

    Code:
        Variable |       Obs        Mean    Std. Dev.       Min        Max
    -------------+--------------------------------------------------------
          maxVal |         1           2           .          2          2
    
    the network is weigthed
    So I have capture the maximum value of the matrix A and stored it into a Stata variable.
    And the if qualifier returns that the network is weighted, which is the information I'd like to capture.

    However, the Stata variable created is a real variable, what I don't need -and could even be cumbersome, even more because of the missing values-, I'd like to store this value (here 2) in a local variable.

    Is there a way to do it quicker, and nicer, without passing through a real variable?

    I guess I shouldn't use st_store to obtain a local in Stata, but I've try the st_numscalar commands, but it seems to do the other way around, from Stata scalar to mata Matrix.
    Thanks,
    Charlie

  • #2
    Code:
    mata :
    x = 42
    st_numscalar("myx", x)
    st_local("myx", strofreal(x))
    end
    
    display myx
    display `myx'
    Read the help files one more time. st_numscalar(), st_local() and st_matrix() can all be called with one argument or two. They get a scalar (local or matrix) from Stata in the former case and set these respective elements in the latter.

    Best
    Daniel

    Comment


    • #3
      Thank you Daniel, for your fast answer,
      It does work.

      The st_local command is just what I needed.
      Moreover, you revealed why the st_numscalar didn't work for me, because I was then calling a local variable (i.e. with the ` ' marks), leading to a blank -which is logical-.
      Thanks again,
      Best,
      Charlie

      Comment


      • #4
        Pedants' corner:

        "local variable": a term best avoided

        if in Stata is not a function; here it is a command. (What you used is the if command, and not the if qualifier.)

        Conversely st_local() and st_store() are Mata functions!

        More at http://www.stata.com/statalist/archi.../msg01258.html

        Comment

        Working...
        X