How do I change the colors of a matrix image?

조회 수: 3 (최근 30일)
Joseph Lee
Joseph Lee 2020년 5월 25일
편집: Ameer Hamza 2020년 5월 25일
I have a script that will generate a 12x12 matrix of ones and zeros. When I create an image from this the default colours are yellow for one and blue for zero. I want to change this to white for zero and red for one.
Can anyone help me?
Thanks,
Joe
m = zeros(12, 12);
numIterations = 1800;
row = ones(1, numIterations);
col = ones(1, numIterations);
m(row(1), col(1)) = 1;
imagesc(m);
axis('on', 'image');
percentage = 0.40; % 20% of time will be the same
for k = 2 : numIterations
if rand(1) < percentage
% 20% of the time it will stay in the same place.
row(k) = row(k-1);
col(k) = col(k-1);
else
% 80% of the time it will select a new place.
% First get a tentative new location.
row(k) = row(k-1) + randi([-1,1], 1);
col(k) = col(k-1) + randi([-1,1], 1);
while row(k) == row(k-1) && col(k) == col(k-1) || row(k) <= 0 || row(k) > size(m, 1) || col(k) <= 0 || col(k) > size(m, 2)
% Get a new location.
row(k) = row(k-1) + randi([-1,1], 1);
col(k) = col(k-1) + randi([-1,1], 1);
while row(k) <= 0 || row(k) > size(m, 1)
row(k) = row(k-1) + randi([-1,1], 1);
end
while col(k) <= 0 || col(k) > size(m, 2)
col(k) = col(k-1) + randi([-1,1], 1);
end
end
end
m(row(k-1), col(k-1)) = 0; % Clear old location
m(row(k), col(k)) = 1; % Set new location
imagesc(m);
grid on;
caption = sprintf('At Iteration %d, Row = %d, Column = %d', k, row(k), col(k));
title(caption, 'FontSize', 15);
drawnow;
pause(0.8)
end

채택된 답변

Ameer Hamza
Ameer Hamza 2020년 5월 25일
편집: Ameer Hamza 2020년 5월 25일
Use colormap
imagesc(m);
colormap([1 1 1; 1 0 0])
grid on;
  댓글 수: 2
Image Analyst
Image Analyst 2020년 5월 25일
How does this give white background with a red square?
Ameer Hamza
Ameer Hamza 2020년 5월 25일
Oh! That was a mistake. Thanks for pointing out.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Color and Styling에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by