poster
2019-12-10, 20:48
我有一个二维二进制矩阵,想要显示为黑白图。例如,假设我有一个4×4矩阵,如下所示:
1 1 0 1 0 0 1 0 1 1 0 1 1 0 0 0 如何将其绘制为黑白矩阵?我的某些输入二进制矩阵的大小为100 x 9,因此理想情况下,我需要一个可推广到不同大小矩阵的解决方案。
回答:
如果要绘制一个如图所示 (https://www.mathworks.com/help/matlab/ref/pcolor.html#bua4ysn)的填字游戏(带有网格线和黑白方块),可以使用imagesc (https://www.mathworks.com/help/matlab/ref/imagesc.html)函数( 灰色colormap (https://www.mathworks.com/help/matlab/ref/colormap.html#input_argument_d0e131488) )并按如下所示修改axis属性 (https://www.mathworks.com/help/matlab/ref/axes-properties.html) :
mat = [1 1 0 1; 0 0 1 0; 1 1 0 1; 1 0 0 0]; % Your sample matrix [r, c] = size(mat); % Get the matrix size imagesc((1:c)+0.5, (1:r)+0.5, mat); % Plot the image colormap(gray); % Use a gray colormap axis equal % Make axes grid sizes equal set(gca, 'XTick', 1:(c+1), 'YTick', 1:(r+1), ... % Change some axes properties 'XLim', [1 c+1], 'YLim', [1 r+1], ... 'GridLineStyle', '-', 'XGrid', 'on', 'YGrid', 'on'); 这是您应该得到的图像:
https://i.stack.imgur.com/o0lPl.png (https://i.stack.imgur.com/o0lPl.png)
更多&回答... (https://stackoverflow.com/questions/3280705)
1 1 0 1 0 0 1 0 1 1 0 1 1 0 0 0 如何将其绘制为黑白矩阵?我的某些输入二进制矩阵的大小为100 x 9,因此理想情况下,我需要一个可推广到不同大小矩阵的解决方案。
回答:
如果要绘制一个如图所示 (https://www.mathworks.com/help/matlab/ref/pcolor.html#bua4ysn)的填字游戏(带有网格线和黑白方块),可以使用imagesc (https://www.mathworks.com/help/matlab/ref/imagesc.html)函数( 灰色colormap (https://www.mathworks.com/help/matlab/ref/colormap.html#input_argument_d0e131488) )并按如下所示修改axis属性 (https://www.mathworks.com/help/matlab/ref/axes-properties.html) :
mat = [1 1 0 1; 0 0 1 0; 1 1 0 1; 1 0 0 0]; % Your sample matrix [r, c] = size(mat); % Get the matrix size imagesc((1:c)+0.5, (1:r)+0.5, mat); % Plot the image colormap(gray); % Use a gray colormap axis equal % Make axes grid sizes equal set(gca, 'XTick', 1:(c+1), 'YTick', 1:(r+1), ... % Change some axes properties 'XLim', [1 c+1], 'YLim', [1 r+1], ... 'GridLineStyle', '-', 'XGrid', 'on', 'YGrid', 'on'); 这是您应该得到的图像:
https://i.stack.imgur.com/o0lPl.png (https://i.stack.imgur.com/o0lPl.png)
更多&回答... (https://stackoverflow.com/questions/3280705)