绘制矩阵,值作为颜色 [英] Plot a matrix, values as colors

查看:236
本文介绍了绘制矩阵,值作为颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个任意尺寸的随机矩阵,我想为每个值分配颜色(随机地或不随机地),并用数字来绘制矩阵,

I have random matrix with arbitrary dimensions and I want to assign a color for each value (randomly or not) and plot the matrix with numbers like,

到目前为止,我已经做到了,

So far I've done this,

m = 12;
n = 8;
A = randi(5,[m n]);
Arot = flipud(A);
pcolor(Arot);figure(gcf);
for i = 1 : n -1
    for j = 1 : m -1
        text(i + .5 , j + .5 ,num2str(Arot(j,i)),'FontSize',18);
    end
end

这给了我

对于

 A =

 4     4     4     1     2     1     4     2
 5     2     2     3     2     1     1     2
 1     2     1     4     1     2     5     5
 1     3     5     3     1     4     1     3
 3     4     4     4     3     3     3     4
 2     5     2     2     1     1     2     4
 1     3     1     3     5     5     2     4
 5     1     2     4     1     4     1     2
 2     4     5     5     1     3     5     2
 4     2     2     3     4     3     3     4
 3     5     3     2     4     3     3     1
 1     4     5     3     2     4     3     5

但是您可以看到,我已经丢失了A的第一行和最后一列.

but as you can see I've lost first row and last column of A.

实际上,问题是使用pcolor开始的,它给出了mxn输入的(m-1)x(n-1)图.

Actually the problem starts bu using pcolor, which gives an (m-1)x(n-1) plot for mxn input.

有什么建议吗?

谢谢

推荐答案

我只是在pcolor之前填充了矩阵,我认为这是您想要的效果.它起作用的原因来自pcolor的帮助文档,其中指出

I just padded the matrix prior to pcolor and I think it's the effect you wanted. The reason it works comes from the help doc for pcolor, which states that

在默认着色模式多面"下,每个单元格具有恒定的颜色 并且不使用C的最后一行和列.

In the default shading mode, 'faceted', each cell has a constant color and the last row and column of C are not used.

m = 12;
n = 8;
A = randi(5,[m n]);
Arot = flipud(A);
Arot = [ Arot; Arot(end,:) ];
Arot = [ Arot, Arot(:,end) ];
pcolor(Arot);figure(gcf);
for i = 1 : n
  for j = 1 : m
    text(i + .5 , j + .5 ,num2str(Arot(j,i)),'FontSize',18);
  end
end

这篇关于绘制矩阵,值作为颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆