Skip to content
DeepForgeby svx
All problems
HardLinear Algebrala-182

Khatri-Rao Product

Problem

Compute the Khatri-Rao product of A (m x k) and B (n x k): the column-wise Kronecker product.

Column j of the result is kron(A[:, j], B[:, j]), giving an (m*n) x k matrix with entries grouped by (i, r) row-major.

Hint

Match columns by index and apply the Kronecker product column by column.

Test cases

case 1
in: [[[1,2],[3,4]],[[5,6],[7,8]]]
expected: [[5,12],[7,16],[15,24],[21,32]]
case 2
in: [[[1],[2]],[[3],[4]]]
expected: [[3],[4],[6],[8]]
case 3
in: [[[1,0]],[[1,1],[1,1]]]
expected: [[1,0],[1,0]]
case 4
in: [[[2,1],[0,3]],[[1,2]]]
expected: [[2,2],[0,6]]
Starter codePython
def khatri_rao(A, B):
    # Your code here
    pass

4 test cases · run your code against them in the editor