How do I initialize a n * m colormap matrix
1 次查看(过去 30 天)
显示 更早的评论
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 个)
Guru
2013-7-5
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 个评论
Guru
2013-7-5
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.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Red 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!