MATLAB Answers

How to create a colormap attributing specific colour to each element?

1 view (last 30 days)
Cassio Lima on 26 Jan 2019
Commented: Cassio Lima on 28 Jan 2019
Hi everyone!
I have an array A,
A = [ 1 2 3 4
2 2 3 1
1 1 4 2];
I want to create a colormap but each element needs to be colored in a specific colour, for example, 1 is blue, 2 is black, 3 is green and 4 is yellow.
Thanks
1 CommentShowHide None

Sign in to comment.

Accepted Answer

Matt Gaidica on 26 Jan 2019
Given these data, I would do this.
A = [ 1 2 3 4
2 2 3 1
1 1 4 2];
cmap = jet(max(A(:)));
figure;
imagesc(A);
colormap(cmap);
colorbar;
0 CommentsShowHide -1 older comments

Sign in to comment.

More Answers (1)

Stephen Cobeldick on 27 Jan 2019
Edited: Stephen Cobeldick on 27 Jan 2019
A = [1,2,3,4;2,2,3,1;1,1,4,2];
map = [0,0,1;0,0,0;0,1,0;1,1,0]; % [blue;black;green;yellow]
imagesc(A,[1,4]) % better to set the color limits.
colormap(map)
1 CommentShowHide None
Cassio Lima on 28 Jan 2019
Thank you guys. It helped a lot :D

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!