简体   繁体   English

MATLAB:如何将矩阵绘制为nxn表?

[英]MATLAB: how to plot a matrix as an nxn table?

I have a routine that iteratively changes the structure of a matrix and I would like to animate the process, so that a user can actually see the structure changing. 我有一个例程来迭代地更改矩阵的结构,并且希望对过程进行动画处理,以便用户可以实际看到结构的变化。

If my matrix is nxn, I want to display the matrix as an nxn table 如果我的矩阵是nxn,我想将矩阵显示为nxn表

eg 例如

在此处输入图片说明

Then using the 'plot' command I would like to have a figure containing: 然后,使用“绘图”命令,我想要一个包含以下内容的图形:

在此处输入图片说明

(gridlines not necessary) My actual matrix could be 25x25 / 100x100 (不需要网格线)我的实际矩阵可能是25x25 / 100x100

This short piece of code 这段简短的代码

n = 25 ;
A = randi(100, n, n) ;

figure ;
xlim([0 n+2]) ;
ylim([0 n+2]) ;
axis off ;
tt = cell(n,n) ;

for i = 1:n
    for j = 1:n
        tt{i,j} = text(j,n-i,sprintf('%d',A(i,j))) ;
    end
end

for k = 1:100
    % Get random coordinates
    i = randi(n) ;
    j = randi(n) ;
    % Get random value
    v = randi(100) ;
    % Remove & update text
    delete(tt{i,j}) ;
    tt{i,j} = text(j,n-i,sprintf('%d',v)) ;
    % Force figure plot
    drawnow() ;
    % Wait a bit
    pause(0.02) ;
end

prints the matrix on a grid, without the lines. 在没有线条的网格上打印矩阵。 It then randomly finds coordinates and change their value. 然后,它随机找到坐标并更改其值。 I'm not sure adding lines help clarity. 我不确定添加行是否有助于清晰度。 If really needed, you can simply add them using plot. 如果确实需要,您可以使用plot简单地添加它们。

You can adjust the sprintf so that it displays your data nicely. 您可以调整sprintf,以使其很好地显示您的数据。 If number are all on top of each other, you can increase the size of the figure by simply dragging the corner of the window. 如果数字彼此重叠,则只需拖动窗口的一角即可增加图形的大小。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM