# Name for this matrix operation?

Suppose two matrices have the same number of rows. I want to perform an operation of element-wise product between all possible column pairs between the two matrices. For example, if

$A = \left[{\begin{array}{ccc} 1 & 2 \\ 3 & 4 \end{array}}\right], B=\left[{\begin{array}{ccc} 5 & 6 & 7 \\ 8 & 9 & 10 \end{array}}\right],$

then the operation leads to a matrix of 6 columns,

$C = \left[{\begin{array}{ccc} 5 & 6 & 7 & 10 & 12 & 14\\ 24 & 27 & 30 & 32 & 36 & 40 \end{array}}\right].$

My question is: Is there a name (and notation) for this kind of matrix operation, similar to Hadamard product?

The background for the question is that such operation seems to be involved in constructing the interaction columns between two dummy-coded factors.

-

## migrated from stats.stackexchange.comAug 30 '13 at 1:48

This question came from our site for people interested in statistics, machine learning, data analysis, data mining, and data visualization.

Quite obvious, that the 1st row of C is the kronecker product of the 1st rows of A and B. The 2nd row - likewise. –  ttnphns Aug 29 '13 at 21:20
That's an interesting observation. Any more concise name or description? –  bluepole Aug 29 '13 at 21:25
Because there are many ways to describe this operation (outer products, tensor products, etc), and it's really about a purely mathematical question, I think the math community may (a) have more interest in it and (b) be able to provide a broad and insightful collection of useful answers. –  whuber Aug 30 '13 at 1:48

No there is not a defined and studied compact matrix operation that does this - "this" being a combination of column-wise Hadamard multiplication and matrix augmentation, which uses the symbol "|". Your operation requires conformable row dimensions. Using small letters to denote the columns of a matrix we have $$A_{k\times n} = \begin{matrix} [a_1 & ...& a_n] \end{matrix}, \qquad B_{k\times m}=\begin{matrix}[b_1 & ...& b_m]\end{matrix}$$

$$C_{k\times (n\times m)} = \Big [a_1*b_1|...|a_1*b_m|...|a_n*b_1|...|a_n*b_m\Big]$$

-

I don't know what you would call this, but here's a quick way you can do this in R:

a = t(matrix(c(1,2,3,4), nrow=2))
b = t(matrix(c(5,6,7,8,9,10), ncol=2))

matrix(apply(a,2, function(x) x * b)
,ncol(a)
,ncol(a)*ncol(b))

-
Thanks for the suggestion! I know how to generate the product in R, but I'm interested in knowing its name, if existing, and its notation. –  bluepole Aug 29 '13 at 21:21

I am not sure what that matrix operator is called, but it is very close to being a Kronecker product (i.e., $A\otimes B$ ) so maybe there is something similar to that? In either case here is some fast code for getting it out of R using Kronecker products as well.

> kronecker(A,B)
[,1] [,2] [,3] [,4] [,5] [,6]
[1,]    5    6    7   10   12   14
[2,]    8    9   10   16   18   20
[3,]   15   18   21   20   24   28
[4,]   24   27   30   32   36   40

> kronecker(A,B)[c(1,4),]
[,1] [,2] [,3] [,4] [,5] [,6]
[1,]    5    6    7   10   12   14
[2,]   24   27   30   32   36   40

-
Another interesting idea! So, to generalize your approach, it would be something like kronecker(A,B)[c(1,4,9,16,...),] if there are more than two rows? Still looking for a more generic idea... –  bluepole Aug 29 '13 at 21:39
@bluepole, please be aware that doing row-by-row kronecker via looping may be faster than performing one kronecker on the whole matrices and then shacking off uncecessary rows. Try it. –  ttnphns Aug 29 '13 at 21:46