How to get different colours in a matrix with imagesc?

3 views (last 30 days)
Hello,
i created a matrix and within the matrix there is a circle of red dots.
i need all the dots to have different colours (red, blue, green) on a random basis, but i dont know how to do it.
here is my code:
m1 = zeros(400,400,3);
center = round(size(m1)/2);
ecc = (rand(1,1000).^0.5).*100;
pol = rand(1,1000).*360;
x = round(cosd(pol) .* ecc); y = round(sind(pol) .* ecc);
m1(sub2ind(size(m1),center(1)-y, center(2)+x)) = 3;
figure; imagesc(m1)
i want to do it with a "for" loop like this
for i = 1:3
ecc = (rand(1,1000).^0.5).*100, i;
pol = rand(1,1000).*360, i;
end
or something like this:
for i = 1:3
ecc = (rand(1,1000, i).^0.5).*100;
pol = rand(1,1000, i).*360;
end
i am very new to matlab and i even fail to figure out where in this code the colour can be placed.
i just know that colour for a very simple matrix works like this:
mat(3,4,1) = 1; %red
mat(5,6,2) = 1; %green
mat(1,2,3) = 1; %blue
thanks for help :)

Accepted Answer

jonas
jonas on 10 Nov 2018
Edited: jonas on 10 Nov 2018
I made an attempt at fixing your code. What you describe with colors is for RGB images, to display with imshow not imagesc. For imagesc you can simply work with 2D matrices and a suitable colormap.
m1 = zeros(400,400,1);
center = round(size(m1)/2);
ecc = (rand(1,1000).^0.5).*100;
pol = rand(1,1000).*360;
x = round(cosd(pol) .* ecc); y = round(sind(pol) .* ecc);
m1(sub2ind(size(m1),center(1)-y, center(2)+x)) = randi([1 3],1,1000);
figure;
colormap([0 0 0;1 0 0;0 1 0;0 0 1])
imagesc(m1)

More Answers (1)

Dave
Dave on 11 Nov 2018
Thanks a lot for your help Jonas!

Products


Release

R2018b

Community Treasure Hunt

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

Start Hunting!