KHATRI_RAO

Computes the Khatri-Rao product (column-wise Kronecker product) of two matrices. If A and B both have N columns, the result is a matrix with N columns where the j-th column is the Kronecker product of the j-th columns of A and B.

Excel Usage

=KHATRI_RAO(matrix_a, matrix_b)
  • matrix_a (list[list], required): First input matrix.
  • matrix_b (list[list], required): Second input matrix.

Returns (list[list]): 2D array representing the Khatri-Rao product.

Example 1: Khatri-Rao product of 2x2 matrices

Inputs:

matrix_a matrix_b
1 2 5 6
3 4 7 8

Excel formula:

=KHATRI_RAO({1,2;3,4}, {5,6;7,8})

Expected output:

Result
5 12
7 16
15 24
21 32
Example 2: Khatri-Rao product for 3x2 and 2x2 matrices

Inputs:

matrix_a matrix_b
1 2 2 1
3 4 0 3
5 6

Excel formula:

=KHATRI_RAO({1,2;3,4;5,6}, {2,1;0,3})

Expected output:

Result
2 2
0 6
6 4
0 12
10 6
0 18
Example 3: Khatri-Rao product with single-column matrices

Inputs:

matrix_a matrix_b
2 1
4 3
5

Excel formula:

=KHATRI_RAO({2;4}, {1;3;5})

Expected output:

Result
2
6
10
4
12
20
Example 4: Khatri-Rao product with sparse columns

Inputs:

matrix_a matrix_b
1 0 2 0
0 1 0 2

Excel formula:

=KHATRI_RAO({1,0;0,1}, {2,0;0,2})

Expected output:

Result
2 0
0 0
0 0
0 2

Python Code

import numpy as np
from scipy.linalg import khatri_rao as scipy_khatri_rao

def khatri_rao(matrix_a, matrix_b):
    """
    Compute the Khatri-Rao product of two matrices.

    See: https://docs.scipy.org/doc/scipy/reference/generated/scipy.linalg.khatri_rao.html

    This example function is provided as-is without any representation of accuracy.

    Args:
        matrix_a (list[list]): First input matrix.
        matrix_b (list[list]): Second input matrix.

    Returns:
        list[list]: 2D array representing the Khatri-Rao product.
    """
    try:
        def to2d(x):
            return [[x]] if not isinstance(x, list) else x

        matrix_a = to2d(matrix_a)
        matrix_b = to2d(matrix_b)

        if not isinstance(matrix_a, list) or not matrix_a or not all(isinstance(row, list) for row in matrix_a):
            return "Error: matrix_a must be a non-empty 2D list"
        if not isinstance(matrix_b, list) or not matrix_b or not all(isinstance(row, list) for row in matrix_b):
            return "Error: matrix_b must be a non-empty 2D list"
        if any(len(row) == 0 for row in matrix_a):
            return "Error: matrix_a rows must be non-empty"
        if any(len(row) == 0 for row in matrix_b):
            return "Error: matrix_b rows must be non-empty"
        if len({len(row) for row in matrix_a}) != 1:
            return "Error: matrix_a must have consistent row lengths"
        if len({len(row) for row in matrix_b}) != 1:
            return "Error: matrix_b must have consistent row lengths"

        try:
            a = np.array(matrix_a, dtype=float)
            b = np.array(matrix_b, dtype=float)
        except (ValueError, TypeError):
            return "Error: matrix inputs must contain numeric values"

        if not np.all(np.isfinite(a)) or not np.all(np.isfinite(b)):
            return "Error: matrix inputs must contain only finite numbers"

        if a.shape[1] != b.shape[1]:
            return "Error: Matrices must have the same number of columns"

        try:
            res = scipy_khatri_rao(a, b)
        except Exception as e:
            return f"Error: {str(e)}"

        return res.tolist()

    except Exception as e:
        return f"Error: {str(e)}"

Online Calculator

First input matrix.
Second input matrix.