function img = write_image(matrix, filename)
% Given a grey level image-like plot, this function saves it as
% an image with a chosen colourmap

% Normalise the image:
t_img = round(255*(matrix-min(matrix(:)))/(max(matrix(:)) - min(matrix(:))));

img = zeros(size(matrix,1), size(matrix,2), 3);

colour_map = hot(256);

for r=1:size(img,1)
    for c=1:size(img,2)
        img(r,c,:) = colour_map(t_img(r,c)+1, :);
    end
end

if nargin>1
    imwrite(img, filename);
end