Announcement

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

  • Matrix of sums of submatrices

    I have matrix A. I'd like to create a new matrix B where each element is the sum of a corresponding 60x60 submatrix of A. By sum I mean the sum of each element of the submatrix. How can I do that?

  • #2
    Here's an approach that I would use. I illustrate it with a toy example for brevity's sake.
    Code:
    version 17.0
    
    clear *
    
    mata:
    
    A = 1, 2, 3, 4 \ 5, 6, 7, 8 \ 9, 10, 11, 12 \ 13, 14, 15, 16
    A
    
    A[| 1,1\2,2 |]
    A[| 1,3\2,4 |]
    A[| 3,1\4,2 |]
    A[| 3,3\4,4 |]
    
    // Begin here
    B = sum(A[| 1,1\2,2 |]), sum(A[| 1,3\2,4 |]) \
        sum(A[| 3,1\4,2 |]), sum(A[| 3,3\4,4 |])
    B
    
    end
    
    exit

    Comment


    • #3
      Perfect, thank you!

      Comment

      Working...
      X