Announcement

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

  • How to convert a single scalar value into a large matrix form

    Say I have a value of 2

    How can I convert this into say a [50,1] matrix such that it is the same size to do some addition/subtraction with my other matrix

    Currently I only know using mat X = (2\2\2\2), but I don't want to have to forward slash 50 times, is there a faster way?

  • #2
    My understanding is that you want a matrix in which each element has the value 2. For this purpose, see -help matrix functions-, where you will find documentation for the obscurely named but important function J(), to be used as follows;
    Code:
    mat X = J(50, 1, 2)
    If you are doing a lot of matrix work, you might find Stata's Mata language useful for your ultimate purpose. Mata includes a syntax in which prefixing an arithmetic operator with a colon will apply that operation to each element of the matrix:
    Code:
    : mata
    : X = 1\2\3\4\5
    : X
           1
        +-----+
      1 |  1  |
      2 |  2  |
      3 |  3  |
      4 |  4  |
      5 |  5  |
        +-----+
    
    : X = X :- 2
    
    : X
            1
        +------+
      1 |  -1  |
      2 |   0  |
      3 |   1  |
      4 |   2  |
      5 |   3  |
        +------+

    Comment


    • #3
      Duplicate post. Asked and answered at https://www.statalist.org/forums/for...a-large-matrix.

      Added: Crossed with #2.

      Comment

      Working...
      X