How can I combine an array of values and mask into a matrix, which contains each value from the array exactly once, but only in places where the mask is non-zero? You could say I want to fill-in the mask-matrix with values from the array.
I don't actually care about the order of the original elements in the result.
mask = [[0, 1, 0], [1, 0, 1], [0, 1, 0]]
values = [1, 2, 3, 4]
result = some_magical_operation(mask, values)
# output: result = [[0, 1, 0], [2, 0, 3], [0, 4, 0]]
# alternative output: result2 = [[0, 2, 0], [1, 0, 3], [0, 4, 0]]
# alternative output: result3 = [[0, 4, 0], [3, 0, 2], [0, 1, 0]]
Note: my actual matrices are significantly less dense than this.