How to get different colours in a matrix with imagesc?

10 次查看(过去 30 天)
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 :)

采纳的回答

jonas
jonas 2018-11-10
编辑:jonas 2018-11-10
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)

更多回答(1 个)

Dave
Dave 2018-11-11
Thanks a lot for your help Jonas!

类别

Help CenterFile Exchange 中查找有关 Large Files and Big Data 的更多信息

产品


版本

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by