【问题标题】:How can I display a 2D binary matrix as a black & white plot?如何将二维二进制矩阵显示为黑白图?
【发布时间】:2011-03-17 21:06:43
【问题描述】:

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

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

如何将其绘制为黑白矩阵?我的一些输入二进制矩阵的大小为 100×9,因此理想情况下,我需要一个能够泛化到不同大小矩阵的解决方案。

【问题讨论】:

    标签: matlab matrix plot


    【解决方案1】:

    不妨试试spy 函数。

    【讨论】:

      【解决方案2】:

      我不确定你的问题是否正确,但你可以试试图像功能,如下所示:

      A = [ 1 1 0; 1 0 1; 1 1 1 ];
      colormap([0 0 0; 1 1 1 ]);
      image(A .* 255);
      

      【讨论】:

      【解决方案3】:

      如果你想制作一个填字游戏类型的绘图as shown here(带有网格线和黑白方块),你可以使用imagesc函数,一个gray colormap,然后像这样修改axes properties

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

      这是你应该得到的图像:

      【讨论】:

        猜你喜欢
        • 2013-12-17
        • 1970-01-01
        • 2021-12-09
        • 2019-05-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-04-16
        相关资源
        最近更新 更多