How to put the values into the specific area and put color

조회 수: 1 (최근 30일)
Jassy
Jassy 2019년 4월 24일
답변: Akira Agata 2019년 4월 24일
A = zeros(70);
I have 168 values is 1-4
Ex Values = { 1, 2, 3, 1, 2, 1, 1, 1, .....}
location is (15,15),(16,15),(17,15), ......... (27,15)
(15,16),(16,16),(17,16),............... (27,16)
(15,17),(16,17),(17,17),.................(27,17)
.
.
.
(15,27),(16,27),(17,27),............... (27,27)
and I want to input color on this location
1 = red
2 = green
3 = blue
4 = black
How to make this in loop?
Thank you.

채택된 답변

Akira Agata
Akira Agata 2019년 4월 24일
You don't need to use for-loop. The following is one possible solution.
A = zeros(70);
% Sample 168-by-1 random array with 1-4.
Values = randi(4,168,1);
% Sample 168-by-2 random array of location
[row,col] = ind2sub(size(A),randperm(numel(A),168));
Location = [row',col'];
% Create label matrix
Label = A;
Label(sub2ind(size(A),Location(:,1),Location(:,2))) = Values;
% Create RGB image
cmap = [...
1 0 0;... % red
0 1 0;... % green
0 0 1;... % blue
0 0 0]; % black
RGB = label2rgb(Label,cmap);
dots.png

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Descriptive Statistics에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by