3
$\begingroup$

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.

$\endgroup$
3
  • $\begingroup$ Quite obvious, that the 1st row of C is the kronecker product of the 1st rows of A and B. The 2nd row - likewise. $\endgroup$
    – ttnphns
    Aug 29, 2013 at 21:20
  • $\begingroup$ That's an interesting observation. Any more concise name or description? $\endgroup$
    – bluepole
    Aug 29, 2013 at 21:25
  • $\begingroup$ 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. $\endgroup$
    – whuber
    Aug 30, 2013 at 1:48

3 Answers 3

1
$\begingroup$

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
$\endgroup$
2
  • $\begingroup$ 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... $\endgroup$
    – bluepole
    Aug 29, 2013 at 21:39
  • $\begingroup$ @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. $\endgroup$
    – ttnphns
    Aug 29, 2013 at 21:46
1
$\begingroup$

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]$$

$\endgroup$
0
$\begingroup$

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))
$\endgroup$
1
  • $\begingroup$ 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. $\endgroup$
    – bluepole
    Aug 29, 2013 at 21:21

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .