How to display a matrix as a image ?

2 views (last 30 days)
my matrix of dimension 120x50 has elements ranging from -1 to 200 . I want to display this matrix as a image in which the matrix cells containing -1 should be displayed as black and 0 should be displayed as white and all positive numbers by red(or green) How to do it ?

Accepted Answer

Azzi Abdelmalek
Azzi Abdelmalek on 6 Jul 2015
Edited: Azzi Abdelmalek on 6 Jul 2015
A=randi([-1 200],120,50)
map=[0 0 0;1 1 1;1 0 0];
idx1=A==-1
idx0=A==0
idx2=A>0
A(idx1)=1
A(idx0)=2
A(idx2)=3
image(A)
colormap(map)

More Answers (1)

Image Analyst
Image Analyst on 6 Jul 2015
Try this, which works just by changing display parameters and colormap, and without changing any values in your image variable:
% Create sample image.
dblImage = randi([-1 200],120,50);
% Initialize the colormap to all red.
myColormap = [ones(202, 1), zeros(202, 2)];
% Set -1 to black
myColormap(1,:) = [0,0,0];
% Set 0 to white
myColormap(2,:) = [1,1,1];
% Display image
imshow(dblImage, [-1, 200], 'InitialMagnification', 500);
axis on;
% Apply the colormap.
colormap(myColormap);
caxis([-1, 200]); % Colormap applies between -1 and 200.
colorbar;

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!