gpt4 book ai didi

matlab - 如何将二维二进制矩阵显示为黑白图?

转载 作者:太空宇宙 更新时间:2023-11-03 19:10:40 25 4
gpt4 key购买 nike

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

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

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

最佳答案

如果你想做一个填字游戏类型的情节as shown here (带有网格线和黑白方 block )您可以使用 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');

这是你应该得到的图像:

enter image description here

关于matlab - 如何将二维二进制矩阵显示为黑白图?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3280705/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com