Announcement

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

  • Using mata to create matrix

    I'd like to create a matrix shown in the figure. It's a NC*(N+C) matrix. All N block matrixes on the left are C*C identity matrix, and all N block matrixes on the right are C*N matrixes. For the block matrixes on the right, the 1st matrix has the 1st column of 1 and the 2nd matrix has the 2nd column of 1 and so on until the nth matrix with the nth column of 1 and all other elements are 0.

    For example ,if N=20 and C=30, then the matrix is 600*50. then all 20 left hand side identity matrixes are 30*30 and all 20 right hand side matrixes are 30*20 with the corresponding column are 1. how could I create this matrix by mata?

  • #2
    Dear Weidi
    No picture is attached.
    For example purposes choose smaller to visualize.
    However, Mata is extremely powerful when creating block matrices.
    See -help m4_standard- and -help m4_manipulation-

    Consider eg the Mata functions J and I ():
    Code:
    :
    mata:
    J(1,3, I(2))
           1   2   3   4   5   6
        +-------------------------+
      1 |  1   0   1   0   1   0  |
      2 |  0   1   0   1   0   1  |
        +-------------------------+
    
    : J(1,3,(J(3,1,1), J(3,3,0))), J(3,1,1)
            1    2    3    4    5    6    7    8    9   10   11   12   13
        +------------------------------------------------------------------+
      1 |   1    0    0    0    1    0    0    0    1    0    0    0    1  |
      2 |   1    0    0    0    1    0    0    0    1    0    0    0    1  |
      3 |   1    0    0    0    1    0    0    0    1    0    0    0    1  |
        +------------------------------------------------------------------+
    end
    Kind regards

    nhb

    Comment


    • #3
      Hi Niels, thank you for your reply and advice. I uploaded the picture by pdf here.
      Attached Files

      Comment


      • #4
        Hi Niels, I used the I() and J() commands, they are really useful, thank you! But, as in the pdf picture, all block matrices on the right has different column of 1. If N are really large, typing the code over and over again is still a pain. Is there some simple way to do it?

        Comment


        • #5
          Hi Weidi
          I think this is what you need
          Code:
          mata:
          : c = 3; n = 5
          
          : J(n,1,I(c)), I(n) # J(c,1,1)
                  1   2   3   4   5   6   7   8
               +---------------------------------+
             1 |  1   0   0   1   0   0   0   0  |
             2 |  0   1   0   1   0   0   0   0  |
             3 |  0   0   1   1   0   0   0   0  |
             4 |  1   0   0   0   1   0   0   0  |
             5 |  0   1   0   0   1   0   0   0  |
             6 |  0   0   1   0   1   0   0   0  |
             7 |  1   0   0   0   0   1   0   0  |
             8 |  0   1   0   0   0   1   0   0  |
             9 |  0   0   1   0   0   1   0   0  |
            10 |  1   0   0   0   0   0   1   0  |
            11 |  0   1   0   0   0   0   1   0  |
            12 |  0   0   1   0   0   0   1   0  |
            13 |  1   0   0   0   0   0   0   1  |
            14 |  0   1   0   0   0   0   0   1  |
            15 |  0   0   1   0   0   0   0   1  |
               +---------------------------------+
          
          : end
          Kind regards

          nhb

          Comment


          • #6
            Thank you so much! It is what need.

            Comment

            Working...
            X