Announcement

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

  • Conformability error when adding a scalar to a matrix

    Can someone please tell me why there is a conformability error? Can Stata not add a scalar to a matrix?
    P.S. I have Stata 12MP


    . clear all

    . matrix mat1 = (0,50,100)

    . scalar scalar1 = 50

    . matrix mat2 = mat1 + scalar1
    conformability error
    r(503);

    . matrix dir
    mat1[1,3]

    . scalar list _all
    scalar1 = 50



  • #2
    Hi Mauricio
    The short answer is. No. At least not the way you are trying.
    It may be easier if you try to do it in Mata tho:
    Code:
    mata:mat1 = (0,50,100)
    mata:scalar1=50
    mata:mat1 
    mata:mat1 =mat1:+scalar1
    mata:mat1
    HTH

    Comment


    • #3
      20 and more years ago some of us cared about these limitations: and wrote some add-ons

      Code:
       
      STB-56 dm79 . . . . . . . . . . . . . . . . . . Yet more new matrix commands
      (help matcorr, matewmf, matvsort, svmat2 if installed) . . N. J. Cox
      7/00 pp.4--8; STB Reprints Vol 10, pp.17--23
      commands to produce a correlation matrix, elementwise monadic
      function of another matrix, selected subsets of matrix rows
      and columns, vec or vech of a matrix, elements sorted within
      a vector, matrix from a vector, and commands to save matrices
      see mata matrix language incorporated into Stata 9.0
      
      
      STB-50 dm69 . . . . . . . . . . . . . . . . . . Further new matrix commands
      (help matdelrc, matewm, matmad, matpow if installed) . . . N. J. Cox
      7/99 pp.5--9; STB Reprints Vol 9, pp.29--34
      collection of new matrix commands providing additional matrix
      checking, management, element-wise operators, maximum absolute
      difference, and power
      
      STB-39 dm49 . . . . . . . . . . . . . . . . . . . . Some new matrix commands
      (help matfunc, varfunc if installed) . . . . . . . . . . . J. Weesie
      9/97 pp.17--20; STB Reprints Vol 7, pp.43--48
      collection of new matrix commands; several for explicit matrices
      and a few for implicit matrices (i.e., variables)
      see mata matrix language incorporated into Stata 9
      and you can find matmps lurking in these ragbags.

      Code:
      -----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
      help for matmps                                                 (STB-50: dm69)
      -----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
      
      Addition of matrix and scalar
      -----------------------------
      
          matmps matrix1 scalar matrix2
          matmps scalar matrix1 matrix2
      
      
      Description
      -----------
      
      matmps calculates the sum of a scalar and a matrix and places it in a
      second matrix. For a scalar x and a matrix A, the second matrix B has
      typical element
      
          B[i,j] =  x + A[i,j].
      
      The order of the arguments is immaterial.
      
      matrix2 may overwrite matrix1.
      
      
      Example
      -------
      
          . matmps 1 A B
      
      
      Author
      ------
      
               Nicholas J. Cox
               University of Durham, U.K.
               [email protected]
      
      
      Also see
      --------
      
          STB:  STB-50 dm69
      but now in 2020 there is different advice at least from me.

      1. Stata's matrix language is very limited and will allow the addition of a 1x1 matrix and a scalar: but not even a vector and a scalar, so just promote the scalar to a vector:

      Code:
      . matrix wanted = (0, 50, 100) + J(1, 3, 50)
      
      . mat li wanted
      
      wanted[1,3]
           c1   c2   c3
      r1   50  100  150
      2. Switch to Mata!

      Code:
      . mata : (0, 50, 100) :+ 50
               1     2     3
          +-------------------+
        1 |   50   100   150  |
          +-------------------+





      Last edited by Nick Cox; 27 Jan 2020, 11:27.

      Comment

      Working...
      X