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).
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).
Comment