Announcement

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

  • Matrix operations

    Hello,
    Assume I have matrix A and B defined below, and I want to gain matrix C; consists of elements of matrix B divided by sum of elements of matrix B.
    Given that my real matrices are a lot larger than this example, how can I make matrix C without having to have to do it one by one?

    Code:
    matrix A=(1\2\3\4)
    
    matrix B=(7,8)
    
    matrix C=(7/(1+2), 8/(3+4))
    Thank you very much for your help,
    Emma

  • #2
    Here is one way:
    Code:
    matrix A=(1\2\3\4)
    matrix B=(7,8)
    mat a = (1,1,0,0)'
    mata : st_matrix("b", sort(st_matrix("a"), 1))
    mat  A = ((a,b)'*A)'
    mata: st_matrix("C", st_matrix("B") :/ st_matrix("A"))
    matrix list C

    Comment


    • #3
      No need to transpose matrices

      Code:
      matrix A=(1\2\3\4)
      matrix B=(7,8)
      mata: A = st_matrix("A")
      mata: B = st_matrix("B")
      mata: st_matrix("C",(B[1,1]/colsum(A[1::2]), B[1,2]/colsum(A[3::4])))

      Comment

      Working...
      X