image
displays the input array as an image. When that input is a matrix, by default image
has the CDataMapping
property set to 'direct'
. This means that each value of the input is interpreted directly as an index to a color in the colormap, and out of range values are clipped:
image(C)
[...] When C
is a 2-dimensional M
xN
matrix, the elements of C
are used as indices into the current colormap
to determine the color. The
value of the image object's CDataMapping
property determines the
method used to select a colormap entry. For 'direct'
CDataMapping
(the default), values in C
are treated as colormap indices (1-based if double, 0-based if uint8 or uint16).
Since Matlab colormaps have 64 colors by default, in your case this has the effect that values above 64 are clipped. This is what you see in your image
graphs.
Specifically, in the first figure the colormap is the default parula
with 64 colors; and in the second figure colormap('gray')
applies a gray colormap of 64 gray levels. If you try for example colormap(gray(256))
in this figure the image range will match the number of colors, and you'll get the same result as with imagesc
.
imagesc
is like image
but applying automatic scaling, so that the image range spans the full colormap:
imagesc(...)
is the same as image(...)
except the data is scaled to use the full colormap.
Specifically, imagesc
corresponds to image
with the CDataMapping
property set to 'scaled'
:
image(C)
[...] For 'scaled'
CDataMapping
, values in C
are first scaled according to the axes CLim
and then the result is treated as a colormap index.
That's why you don't see any clipping with imagesc
.