Announcement

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

  • Divide skalar/locals and input result into matrix

    Dear all,
    I need to devide one scalar/local through another and input the result into a matrix.
    In the following, you find a simplified version of the syntax, which i use:

    mata: st_numscalar("sum", z)
    count
    local a = r(N)
    scalar define b = `sum'/`a' // (1) here scalar/local one ist divided through another
    matrix define B = (.,.\.,.)
    matrix B[1,1] = `b' // (2) here the result is entered into a matrix

    Unfortuantely, neither step (1) nor (2) works. Can somebody help me to definie a version of the syntax, which works?

  • #2
    A numeric scalar is not a local macro. With that fixed and some other simplifications try

    Code:
    mata: st_numscalar("sum", z)
    count
    scalar b = sum/r(N)
    matrix B = (.,.\.,.)
    matrix B[1,1] = b
    or even

    Code:
    mata: st_numscalar("sum", z)
    matrix B = (.,.\.,.)
    matrix B[1,1] = sum/_N
    or even

    Code:
    mata: st_numscalar("sum", z)
    matrix B = (sum/_N, . \ . , .)
    I quite often see programming practice of the following form:

    I have a pen. I put it in a box. I want to use my pen. I take it out of the box.

    With nothing else said, the box part can be omitted.

    In this case, you defined a scalar b. That is the boxing. No harm in that, but nevertheless did you really need the scalar? If you are going to use it later, and especially more than once, that is a good idea, but not otherwise.
    Last edited by Nick Cox; 05 Apr 2019, 04:07.

    Comment


    • #3
      Dear Nick,
      thanks for your quick help, your general advice and special advice to further simplify my syntax!
      The problem is solved in a very elegant way now.
      Best

      Comment

      Working...
      X