How do I initialize a n * m colormap matrix

2 views (last 30 days)
Harry
Harry on 5 Jul 2013
I want to set a blank colormap (preferably all white) at the beginning of my code so elements can be added to it later. Previously I used the gray scale so I use:
M = zeros(m,n);
but that doesn't work anymore because the colormap requires the results to have three elements (r,g,b).
  1 Comment
Harry
Harry on 5 Jul 2013
Also why won't this work:
for j = 1:1023
for i = 1:989
N(j,i) = [1,1,1];
end
end
imagesc(N)
colormap

Sign in to comment.

Answers (1)

Guru
Guru on 5 Jul 2013
Hmm, you do realize that colormap is intended to serve as a link between values of a matrix and what color they should appear as when you use image(), right?
So the size of the colormap should always be mx3 where m is the maximum value of your matrix. To set a colormap to be all white:
map = ones(m,3);
If you have a matrix N that you want to see as an image and don't know what m should be:
m = ceil(max(N(:)));
Since m is used as a dimension for map, it must be an integer.
for j = 1:1023
for i = 1:989
N(j,i) = [1,1,1];
end
end
This statement is invalid because within a nested for-loop, you are trying to assign a vector "[1,1,1]" to an element in N(j,i). So that's an invalid assignment.
  3 Comments
Harry
Harry on 5 Jul 2013
Also how would I go about assigning a particular element of N a particular colour?
Guru
Guru on 5 Jul 2013
Sorry that you didn't know it, but yes that is what colormap does and how it works.
Here's a simple example on colormap and N that will hopefully help you out
% Typing it this way so you might see it without having to run in MATLAB but using data entry mode
N = [1 2
3 5];
map = [0 0 0
1 0 0
0 0 1
0 1 0
1 1 1]
% In map then, the following rows refer to following colors:
% row 1 - black
% row 2 - red
% row 3 - blue
% row 4 - green
% row 5 - white
image(N)
This will display an image separated by quadrants using MATLAB's default colormap (jet), since all values are low, it should appear as squares for the quadrants all different shades of blue.
colormap(map)
This will apply the colormap to the image that represents N. Now you should have an image that looks like this
black red blue white
as MATLAB takes the value of N at that position and corresponds it to the row color defined in the colormap. One way my former colleagues liked to explain colormap is that it is like color by number, if you are familiar with that coloring book activity for children.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!