Announcement

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

  • Merging matrices

    Dear Forum,
    I have a matrix with Leontief Coefficients with a size of 47x47. My aim is to merge the entries into a matrix with a size of 6x6.
    I would love to know if a command exists that allows me to specify groups of entries and adding up the coefficients.
    The code below shows how I started with the first two entries of the new matrix. A more elegant way would be to know commands that help me to add up all intersections of defined rows and columns. Any help would be highly appreciated.

    Kind Regards
    Stefan

    Code:
    import excel using "iotest.xlsx", firstrow;
    mkmat crop-other, matrix(A)  rownames(Sectors) ;
       matrix B=J(6,6,0) ;
       matrix rownames B= food textile energy house transp misc ;
       matrix colnames B= food textile energy house transp misc ;
       matrix B[1,1]=A[1,1]+A[1,2]+A[1,7]+A[1,8]+A[2,1]+A[2,2]+A[2,7]+A[2,8]+A[7,1]+A[7,2]+A[7,7]+A[7,8]+A[8,1]+A[8,2]+A[8,7]+A[8,8]+A[9,9] ;
       matrix B[2,2]=A[10,10]+A[10,11]+A[10,12]+A[11,10]+A[11,11]+A[11,12]+A[12,10]+A[12,11]+A[12,12] ;

  • #2
    So, it looks like each index from 1 to 6 in B corresponds to some subset of indices in A. The subsets are not necessarily consecutive numbers, as 1 in B seems to correspond to 1, 2, 7, 8 in A. It isn't clear what rule, if any, governs this correspondence between B indices and A sets of indices. I'm just going to assume it's arbitrary and has to be spelled out in the code and will leave those specifics to you. I'll also assume that you want to fill in all of B and not just the diagonal, and the off-diagonals use crossed sums. And I assume B is supposed to be symmetric since all the formulas you show would lead to that result. So I think you can do something like this:

    Code:
    import excel using "iotest.xlsx", firstrow
    mkmat crop-other, matrix(A) rownames(Sectors) 
    matrix B=J(6,6,0) 
    matrix rownames B= food textile energy house transp misc 
    matrix colnames B= food textile energy house transp misc 
    
    local a1_indices 1 2 7 8
    local a2_indices 10 11 12
    // HERE YOU HAVE TO DEFINE locals a3_indices THROUGH a6_indices
    
    forvalues i = 1/6 {
        forvalues j = 1/`i' {
            foreach k of local a`i'_indices {
                foreach m of local a`j' indices {
                    matrix B[`i', `j'] = B[`i', `j'] + A[`k', `m']
                }
            }
        }
        forvalues j = `=`i'+1'/6 {
            matrix B[`i', `j'] = B[`j', `i']
        }
    }
    Warning: Code not tested and I have not yet had my morning caffeine. But I think it will work with little or no modification.
    Note: I have written this without semi-colon terminators, as that is my style.

    Comment


    • #3
      Dear Clyde,
      thank you very much for your early morning answer. Your assumptions are correct and the code looks very promising. I will try it out very soon.

      Comment


      • #4
        Dear Clyde,
        your code worked out well and was a lifesaver for me.
        I only had to modify the second part because it resulted in the diagonal and all entries left of it only (only row 6 was complete).
        I did some trial and error and here's the code that resulted in the full matrix:

        Code:
        import excel using "iotest.xlsx", firstrow;
        mkmat crop-other, matrix(A)  rownames(Sectors) ;
           matrix B=J(6,6,0) ;
           matrix rownames B= food textile energy house transp misc ;
           matrix colnames B= food textile energy house transp misc ;
           local a1_indices 1 2 7 8 9 ;
        local a2_indices 10 11 12 ;
        local a3_indices 15 27 3;
        local a4_indices 13 14 16 17 18 19 20 21 22 23 24 25 26 29 4 5 6 28 29 ;
        local a5_indices 30 31 32 33 34 ;
        local a6_indices 35 36 37 38 40 41 42 43 44 45 46 47 ;
        
        // HERE YOU HAVE TO DEFINE locals a3_indices THROUGH a6_indices
        
        forvalues i = 1/6 { ;
            forvalues j = 1/`i' { ;
                foreach k of local a`i'_indices { ;
                    foreach m of local a`j'_indices { ;
                        matrix B[`i', `j'] = B[`i', `j'] + A[`k', `m'] ;
                    } ;
                } ;
        } ;
        } ;
        forvalues i = 1/6 { ;
        forvalues j = `=`i'+1'/6 { ;
                foreach k of local a`i'_indices { ;
                    foreach m of local a`j'_indices { ;
                matrix B[`i', `j'] = B[`i', `j'] + A[`k', `m'];
            } ;
            } ;
        } ;
        };
        Thank you again for your efforts!

        Comment

        Working...
        X