Announcement

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

  • repeating a sequence of values in a matrix

    Hi All,

    I am trying to generate a sequence of 1's in row of a matrix where all other values remain 0. More specifically in the example code below, for the first row, the first 4 columns should be coded 1, in the second row, columns 5 through 8 should be coded 1, in row 3, columns 9 through 12 should be coded 1, etc...

    I am trying to elaborate on the basic matrix code BB[1,1..4] = 1, to no success...

    All help is welcome!

    Ariel



    Code:
    local margcnt = 4
    
    mat BB = J(`margcnt',`margcnt'^2,0)
                    
    forval i = 1/`margcnt' {
        mat FF = BB[`i', ((`i' - 1) * `margcnt' + 1)..(`i' * `margcnt')] = 1
    }

  • #2
    Code:
    local margcnt = 4
    clear matrix
    mat B= I(`margcnt')
    forval i=1/`margcnt'{
        forval j=1/`margcnt'{
            mat BB=nullmat(BB), B[`i', 1...]'
        }
    }
    mat l BB
    Res.:

    Code:
    . mat l BB
    
    BB[4,16]
        r1  r1  r1  r1  r2  r2  r2  r2  r3  r3  r3  r3  r4  r4  r4  r4
    c1   1   1   1   1   0   0   0   0   0   0   0   0   0   0   0   0
    c2   0   0   0   0   1   1   1   1   0   0   0   0   0   0   0   0
    c3   0   0   0   0   0   0   0   0   1   1   1   1   0   0   0   0
    c4   0   0   0   0   0   0   0   0   0   0   0   0   1   1   1   1

    Comment


    • #3
      Thank you, Andrew! That is exactly what I was looking for!

      Ariel

      Comment

      Working...
      X