# How do I initialize a n * m colormap matrix

조회 수: 5 (최근 30일)
Harry 2013년 7월 5일
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이전 댓글 -1개 표시이전 댓글 -1개 숨기기
Harry 2013년 7월 5일
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

댓글을 달려면 로그인하십시오.

### 답변 (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이전 댓글 1개 표시이전 댓글 1개 숨기기
Harry 2013년 7월 5일
Also how would I go about assigning a particular element of N a particular colour?
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 CenterFile Exchange에서 Red에 대해 자세히 알아보기

### Community Treasure Hunt

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

Start Hunting!

Translated by