%% Eliminate Duplicate Entries in Colormap
% Use the magic function to define X as a 4-by-4 array that uses every value in the range between 1 and 16.

X = magic(4);

%
% Use the gray function to create an eight-entry colormap. Then, concatenate the two eight-entry colormaps to create a colormap with 16 entries, map. In map, entries 9 through 16 are duplicates of entries 1 through 8.

map = [gray(8); gray(8)];
size(map)

%
% Use cmunique to eliminate duplicate entries in the colormap.

[Y, newmap] = cmunique(X, map);
size(newmap)

%
% cmunique adjusts the values in the original image X so that Y and newmap produce the same image as X and map.

figure
image(X)
colormap(map)
title('X and map')

figure
image(Y)
colormap(newmap)
title('Y and newmap')

% Copyright 2015 The MathWorks, Inc.