我在大学项目中工作,我需要在图表中绘制矩阵的单元格,并根据其值来填充颜色,但我在颜色方面遇到了很多麻烦矩阵,因为我无法将其用作填充函数的输入。
我的想法是获得类似于下图的内容,但我在代码的坐标中添加了限制。
function [ output_args ] = drawMatrix(Table)
[m,n]=size(Table); %Get the size of the matrix;
X = 1-0.5:1:n+0.5; %Array with the X coordinates of each cell.
Y = 1-0.5:1:m+0.5; %Array with the Y coordinates of each cell.
C = repmat('w',[m,n]); %Color matrix to represent the color of each single cell, originally all in white.
[x,y]=meshgrid(X,Y); %Creates the coordinates of the cells of the matrix.
for a=1:m
for b=1:n
if Table (a,b) == 1
C(a,b)='b'; % If the value of the original cell is 1, the color is changed to blue.
end
end
end
photo = fill(x', y', C)
输入矩阵是:
[0, 0, 1, 1, 0;
0, 1, 1, 0, 0;
0, 0, 1, 0, 0;
1, 0, 0, 0, 0]
我收到此错误:
使用
fill
时出错color/linetype
参数出错。
drawMatrix
中的错误(第20行)
photo = fill(x', y', C);
感谢Sardar Usama,我有能力找到问题的解决方案。随意使用它。
function [ output_args ] = drawMatrix(Table)
colors = [1,1,1;0,0,0]; %RGB colors to use
%(2 in my case,because only has 2 possible values).
values = [0,1]; %The 2 possible values.
imagesc(Table,values); %Creates an image with the matrix Table and the
range of values.
colormap(colors); %
axis off;