I’m writing this up as a note as much for myself as it is for the edification of others. Since nobody seems to clearly explain how to go about doing this in Matlab.
Okay, so you want to make a heatmap plot of something in Matlab. What is that exactly? Well, let’s just say you have a 2-d Gaussian mixture model (GMM) and you want to plot all possible values of its likelihood function. You might end up with something like this:
(Ignore everything except the GMM; this is sample stuff from a paper I’m working on.)
Here’s how you do it, step by step, but slightly truncated at certain steps:
- Generate x and y points using
[X Y] = meshgrid(-100:1:100, -100:1:100);
- Using the
X, generate a matrix of zeros:
M = zeros(size(X));(You can use the size of Y as well, since they should have equal dimensions.)
- Fill in the matrix using values of your function evaluated at
[X(i,j), Y(i,j)]'with i = 1 to the number of rows of X and j = 1 to the number of columns of X; for example:
M(i,j) = p([X(i,j); Y(i,j)]');.
- Use the
image()function to draw your heatmap. Use the following command to do it correctly, given the example parameters above:
image([-100 100], [-100 100], M, 'CDataMapping', 'scaled');
- And you’re done.
Note that this process can take quite some time depending on how many points you generate using
meshgrid(). If you want high resolution heatmaps, though, you’re going to have to generate a lot of points. If you’re able to vectorize or otherwise accelerate the function evaluation process, by all means do so, as that’s where the bulk of your time will be spent.