How to get different colours in a matrix with imagesc?
3 次查看(过去 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 :)
0 个评论
采纳的回答
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)
0 个评论
更多回答(1 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Orange 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!