Announcement

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

  • Multiply matrices only for rows/columns with matching names

    Hello,

    I have converted 2 datasets into matrices.

    The matrices are now as follows:

    Matrix A: Rows are variable r, Columns are countries
    Matrix B: Rows are countries, Columns are variable c

    I would like to multiply the two matrices together. However, the dataset used to create matrix A has a slightly different sample of countries than the dataset used to create matrix B. I only want to multiply the matrices such that each cell in Matrix A within a particular country column is multiplied by a cell in Matrix B within a particular country row.

    Is there a way to easily identify and get rid of the columns in Matrix A without a corresponding row entry in Matrix B (and vice versa)?

    e.g., in the below example, I would want to find some way of identifying that AFG was only in Matrix A, that AUS and CHN were only in Matrix B, and therefore to only multiply together the matrices without these columns/rows:

    Matrix A
    AFG ARG FRA GRE VEN
    r1 3 5 2 2 4
    r2 4 4 7 6 6
    r3 1 8 5 2 1

    Matrix B
    c1 c2 c3 c4
    ARG 3 4 3 3
    AUS 7 2 4 2
    CHN 4 4 3 3
    FRA 3 2 4 8
    GRE 2 4 1 5
    VEN 6 8 7 8

    So I would want to identify and exclude the rows/columns with countries which aren't in both matrices, and so multiply the matrices as follows:

    ARG FRA GRE VEN
    r1 5 2 2 4
    r2 4 7 6 6
    r3 8 5 2 1

    *
    c1 c2 c3 c4
    ARG 3 4 3 3
    FRA 3 2 4 8
    GRE 2 4 1 5
    VEN 6 8 7 8




    Thank you for your help!


    (I am using Stata 14.1 on Windows).

  • #2
    Hi Anna,
    here you go:

    Code:
    matrix A = (3,5,2,2,4\4,4,7,6,6\1,8,5,2,1)
    matrix colnames A = AFG ARG FRA GRE VEN
    matlist A
    matrix B = (3,4,3,3\7,2,4,2\4,4,3,3\3,2,4,8\2,4,1,5\6,8,7,8)
    matrix rownames B = ARG AUS CHN FRA GRE VEN
    matlist B
    
    local a_countries : colnames A
    local b_countries : rownames B
    local countries: list a_countries & b_countries
    display "`countries'"
    local count : word count `countries'
    local last: word `count' of `countries'
    
    
    foreach c of local countries {
    
    matrix A_`c' = A[1...,`""`c'""']
    matrix B_`c' = B[`""`c'""',1...]
    
    }
    *
    local A_all
    foreach j of local countries {
         local A_all `A_all' A_`j',
    }
    di `""`A_all'""'
    
    local A_all : subinstr local A_all "A_`last'," "A_`last'"
    di `""`A_all'""'
    
    local B_all
    foreach j of local countries {
         local B_all `B_all' B_`j'\
    }
    di `""`B_all'""'
    
    local B_all : subinstr local B_all "B_`last'\" "B_`last'"
    di `""`B_all'""'
    *
    matrix A_new = (`A_all')
    matlist A_new
    
    matrix B_new = (`B_all')
    matlist B_new

    Comment


    • #3
      Maria's advice is helpful, but the starting point was poorly chosen. It's better to combine datasets than to try to combine matrices that don't match.

      Comment

      Working...
      X