MATLAB Answers

Jassy
0

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

Asked by Jassy
on 24 Apr 2019
Latest activity Answered by Akira Agata
on 24 Apr 2019
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.

  0 Comments

Sign in to comment.

1 Answer

Answer by Akira Agata
on 24 Apr 2019
 Accepted Answer

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 Comments

Sign in to comment.