Apply a Colour Matrix

~12 mincode completion

Implement apply_color_matrix(image, M) returning an array of the same shape as image.

Examples

Identity matrix leaves every pixel unchanged

Input
apply_color_matrix([[[1, 2, 3], [4, 5, 6]], [[7, 8, 9], [10, 11, 12]]], [[1, 0, 0], [0, 1, 0], [0, 0, 1]])
Output
[[[1, 2, 3], [4, 5, 6]], [[7, 8, 9], [10, 11, 12]]]

Swap-R-and-B matrix on a single pixel

Input
apply_color_matrix([[[1, 2, 3]]], [[0, 0, 1], [0, 1, 0], [1, 0, 0]])
Output
[[[3, 2, 1]]]

Hints

Hint 1

Use a matrix product rather than nested loops, and check which operand transposes.

Hint 2

Watch for this: multiplied on the wrong side.

Requirements

  • image: array of shape (H, W, 3)

  • M: 3x3 matrix, applied to the channel axis

  • Return array of shape (H, W, 3)

Constraints

  • Allowed library: NumPy only

  • Time limit: 200 ms, Memory: 64 MB

Where this shows up

~12 min

8 employers weight this skill

4 autonomy companies, 1 defense company, 1 health and bio company, 1 enterprise vendor, 1 AI product company. Top match scores 93.

Python
import numpy as np


def apply_color_matrix(image, M):
    """
    Apply a 3x3 colour transform to every pixel: p' = M p.

    Args:
        image: array of shape (H, W, 3)
        M:     3x3 matrix, applied to the channel axis

    Returns:
        array of shape (H, W, 3)
    """
    # YOUR CODE HERE
    pass
Loading docs…

The AI Mentor needs an account

It reads your code and the failing tests and nudges you toward the fix without handing you the answer. Free accounts get it on every problem you're working on today.