如何将2D二进制矩阵显示为黑&白色情节? [英] How can I display a 2D binary matrix as a black & white plot?

查看:107
本文介绍了如何将2D二进制矩阵显示为黑&白色情节?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个二维二进制矩阵,要显示为黑白图.例如,假设我有一个4×4矩阵,如下所示:

I have a 2D binary matrix that I want to display as a black and white plot. For example, let's say I have a 4-by-4 matrix as follows:

1 1 0 1
0 0 1 0
1 1 0 1
1 0 0 0

如何将其绘制为黑白矩阵?我的某些输入二进制矩阵的大小为100 x 9,因此理想情况下,我需要一个可推广到不同大小矩阵的解决方案.

How can this be plotted as a black and white matrix? Some of my input binary matrices are of size 100-by-9, so I would ideally need a solution that generalizes to different sized matrices.

推荐答案

如果要制作填字游戏类型的情节,请灰色色图,然后修改轴属性像这样:

If you want to make a crossword-type plot as shown here (with grid lines and black and white squares) you can use the imagesc function, a gray colormap, and modify the axes properties like so:

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');

这是您应该得到的图像:

And here's the image you should get:

这篇关于如何将2D二进制矩阵显示为黑&白色情节?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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