Let's say I have a 2D NumPy array A of shape (n, 3) and a 3D array B of shape (x, y, n). I want to create a 3D array Y of shape (x, y, 3) - which is an RGB image.
The 3rd dimension of B contains probabilities [0, 1] which sum up to 1. Array A is a list of RGB colors (one color for each probability). For each element C in the 3rd dimension of B I want to compute the sum of A * C so that I get a RGB color vector.
Example:
Given a colormap A as
A = [(255 0 0),
( 0 255 0),
( 0 0 255)]
and an element C in the 3rd dimension of B at point (x, y) as
C = [0.2, 0.6, 0.20]
I want to compute C' = sum(A * C)
as
(255 0 0) * 0.2
+ ( 0 255 0) * 0.6
+ ( 0 0 255) * 0.2
---------------------
( 51 153 51)
and assign
Y[x, y, :] = C'
I know I could just iterate over x and y with a for loop and compute each element at a time but I wonder if this can be done in a vectorized manner that I don't have to iterate over the array myself (mainly for performance reasons).