1
votes

So I've got a 10x10 matrix randMat that has random integers between 1 and 4 and another array map of zeros which is to have the RGB values for four colors i.e. a row for each color and three columns for the amounts of red, green, and blue.

randMat = randi(4,10)
map = zeros(4,3);

Using a for loop I've assigned to each row of map the RGB values for a shade of red with red values from 1/4 to 4/4 = 1. So each row should have a red value of i/4 and blue and green values of zero. I've also made map the colormap

for i=1:4
  map(i,1) = i/4;
end

colormap(map);

Now the problem is I want to visualize randMat in 4 shades of red but it keeps showing up as blue. What am I missing?

image(randMat)
axis off
axis square
1
I get red when I run your code. Is something else interfering with your colormap? - siliconwafer
It works perfect for me. You shoud not close the figure that opened after colormap(map), or you can write the colormap after image(randMat) - Adiel
that's odd... so when I put the last part of the code in its own section i.e. using %% it goes blue but without it, it goes red. anyways thanks for helping guys~ - Kennan

1 Answers

0
votes

As you are working with sections, the only explanation I can imagine, is that you don't run the section generating the colormap.

clc
clear
close all

randMat = randi(4,10)
map = zeros(4,3)
%%
for i=1:4
  map(i,1) = i/4;
end

colormap(map);

%%
image(randMat)
axis off
axis square

If you run the first part and then the last part, the image appears blue. If you run all three subsequently it appears red, and of course if you run all together it appears red as well. I know from own experiences, that when working with sections, it happens to often that you just use Crtl+Enter to run a single section. So just make sure to run all relevant parts.