# MATLAB Reverse Colormap

This tutorial will discuss reversing a colormap using the `flipud()` function in MATLAB.

## MATLAB Reverse Colormap

The colormap sets the colors of a graphics object like an image. A colormap is a matrix of values that uses RGB triplet values to assign colors to values of a matrix or array.

In Matlab, an image is stored in a matrix form and contains numbers that define its colors. When we use a function like `imshow()` to show the matrix as an image, the function assigns color values to the image using a colormap.

To reverse a colormap, we can use the `flipud()` function to flip the values of a matrix or array. The colormap is also a matrix containing the values of colors so that we can use the `flipud()` function to reverse the colors present in the colormap.

If we reverse a colormap, the colors present in an image and other objects which Matlab is currently showing will be reversed. For example, let’s create a binary image and reverse its colormap using the `flipud()` function.

See the code below.

``````clc
clear

v = [0 0 0 0;0 1 0 0;0 0 1 0;0 0 0 0];
figure(1)
imshow(v,'InitialMagnification','fit')
colorbar
figure(2)
imshow(v,'InitialMagnification','fit')
colorbar
colormap(flipud(gray))
``````

Output: In the above code, we created two figures of the binary image using the `imshow()` function. The first figure shows the original image and color bar with the original colormap.

Blender - How To Flip Normals

In the second figure, we have reversed the colormap of the first figure. In the output, the left figure shows the image with the original colormap, and the right figure shows the image with the reversed colormap.

We can see in the output that the colors are reversed. The white color is converted to black, and the black color is converted to white.

In the above code, we used the magnification property of the `imshow()` function to fit the given image to the figure because the image is very small, but if the size of the input image is large or we don’t want to magnify the given image, we can neglect it.

In the above example, we used a binary image with only two colors but can also perform this operation with a colored image.

If we want to flip the colormap of a colored image, we have to change the last line of the above code in which we are flipping the gray colormap, but we have to use the colormap in place of gray.

For example, let’s use the `imagesc()` function to create a color image from a matrix and reverse its colormap using the `flipud()` function.

See the code below.

``````clc
clear

v = [0 0 0 0;0 1 0 0;0 0 1 0;0 0 0 0];
figure(1)
imagesc(v)
colorbar
figure(2)
imagesc(v)
colorbar
colormap(flipud(colormap))
``````

Output: The `imagesc()` function displays an image with scaled colors. If the input of this function is a matrix, the function will assign each value a different color from a colormap and display it.

In the above code, we are flipping the previous colormap and assigning the new values to the new colormap.

In the above output, the left figure shows the image with the original colormap, and the right figure shows the image with the reversed colormap. We can see in the above output that the image’s color and the color bar have been changed.

Check this link for more details about the colormap. Check this link for more details about the `flipud()` function.

Write for us
DelftStack articles are written by software geeks like you. If you also would like to contribute to DelftStack by writing paid articles, you can check the write for us page.

## Related Article - MATLAB Colormap

• MATLAB Plot Color Map