![]() |
如何将2D二进制矩阵显示为黑白图?
我有一个二维二进制矩阵,想要显示为黑白图。例如,假设我有一个4×4矩阵,如下所示:
1 1 0 1 0 0 1 0 1 1 0 1 1 0 0 0 如何将其绘制为黑白矩阵?我的某些输入二进制矩阵的大小为100 x 9,因此理想情况下,我需要一个可推广到不同大小矩阵的解决方案。 [B]回答:[/B] 如果要绘制一个[URL="https://www.mathworks.com/help/matlab/ref/pcolor.html#bua4ysn"]如图所示[/URL]的填字游戏(带有网格线和黑白方块),可以使用[URL="https://www.mathworks.com/help/matlab/ref/imagesc.html"]imagesc[/URL]函数( [URL="https://www.mathworks.com/help/matlab/ref/colormap.html#input_argument_d0e131488"]灰色colormap[/URL] )并按如下所示修改[URL="https://www.mathworks.com/help/matlab/ref/axes-properties.html"]axis属性[/URL] : 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'); 这是您应该得到的图像: [URL="https://i.stack.imgur.com/o0lPl.png"][IMG]https://i.stack.imgur.com/o0lPl.png[/IMG][/URL] [url=https://stackoverflow.com/questions/3280705]更多&回答...[/url] |
所有时间均为北京时间。现在的时间是 01:01。 |
Powered by vBulletin
版权所有 ©2000 - 2025,Jelsoft Enterprises Ltd.