How to get different colours in a matrix with imagesc?

조회 수: 10 (최근 30일)
Dave
Dave 2018년 11월 10일
답변: Dave 2018년 11월 11일
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