来说一下Matlab中可视化矩阵的稀疏模式,用spy即可绘制。它可以把一个矩阵以图形的方式绘制到Matlab中,我们可以通过几个参数来改变标记的颜色及矩阵元素的标记大小。本文,将举几个简单的例子,分别描述稀疏模式的绘制、颜色的标记、大小的标记等常用方式。
下面我们将开始非常详细的 Matlab spy 函数语法介绍,实例引用,结果展示。首先,我们给出 Matlab 中关于 spy 函数的帮助文本如下:
>> help spy
spy Visualize sparsity pattern.
spy(S) plots the sparsity pattern of the matrix S.
spy(S,'LineSpec') uses the color and marker from the line
specification string 'LineSpec' (See PLOT for possibilities).
spy(S,markersize) uses the specified marker size instead of
a size which depends upon the figure size and the matrix order.
spy(S,'LineSpec',markersize) sets both.
spy(S,markersize,'LineSpec') also works.
常见用法
spy(S)
spy(S,LineSpec)
spy(___,MarkerSize)
语法说明
spy(S) 绘制矩阵 S 的稀疏模式。非零值是彩色,而零值是白色。该图显示矩阵中的非零元素数,nz = nnz(S)。
spy(S,LineSpec) 还指定 LineSpec,以给出绘图中要使用的标记符号和颜色。例如,spy(A,’r*’) 使用红色星号表示非零。
spy(___,MarkerSize) 指定 MarkerSize,以使用上述任一输入参数组合给出标记的大小。
稀疏模式的绘制
绘制 Buckminster Fuller 多面穹顶的连接图形的 60×60 稀疏邻接矩阵。此矩阵也可表示碳 60 分子和足球。
B = bucky;
spy(B)
我们可以看到bucky为一个稀疏双精度矩阵单元,结果如下:
B =
(2,1) 1
(5,1) 1
(6,1) 1
(1,2) 1
(3,2) 1
(11,2) 1
(2,3) 1
(4,3) 1
(16,3) 1
(3,4) 1
(5,4) 1
(21,4) 1
(1,5) 1
(4,5) 1
(26,5) 1
(1,6) 1
(7,6) 1
(10,6) 1
(6,7) 1
(8,7) 1
(30,7) 1
(7,8) 1
(9,8) 1
(42,8) 1
(8,9) 1
(10,9) 1
(38,9) 1
(6,10) 1
(9,10) 1
(12,10) 1
(2,11) 1
(12,11) 1
(15,11) 1
(10,12) 1
(11,12) 1
(13,12) 1
(12,13) 1
(14,13) 1
(37,13) 1
(13,14) 1
(15,14) 1
(33,14) 1
(11,15) 1
(14,15) 1
(17,15) 1
(3,16) 1
(17,16) 1
(20,16) 1
(15,17) 1
(16,17) 1
(18,17) 1
(17,18) 1
(19,18) 1
(32,18) 1
(18,19) 1
(20,19) 1
(53,19) 1
(16,20) 1
(19,20) 1
(22,20) 1
(4,21) 1
(22,21) 1
(25,21) 1
(20,22) 1
(21,22) 1
(23,22) 1
(22,23) 1
(24,23) 1
(52,23) 1
(23,24) 1
(25,24) 1
(48,24) 1
(21,25) 1
(24,25) 1
(27,25) 1
(5,26) 1
(27,26) 1
(30,26) 1
(25,27) 1
(26,27) 1
(28,27) 1
(27,28) 1
(29,28) 1
(47,28) 1
(28,29) 1
(30,29) 1
(43,29) 1
(7,30) 1
(26,30) 1
(29,30) 1
(32,31) 1
(35,31) 1
(54,31) 1
(18,32) 1
(31,32) 1
(33,32) 1
(14,33) 1
(32,33) 1
(34,33) 1
(33,34) 1
(35,34) 1
(36,34) 1
(31,35) 1
(34,35) 1
(56,35) 1
(34,36) 1
(37,36) 1
(40,36) 1
(13,37) 1
(36,37) 1
(38,37) 1
(9,38) 1
(37,38) 1
(39,38) 1
(38,39) 1
(40,39) 1
(41,39) 1
(36,40) 1
(39,40) 1
(57,40) 1
(39,41) 1
(42,41) 1
(45,41) 1
(8,42) 1
(41,42) 1
(43,42) 1
(29,43) 1
(42,43) 1
(44,43) 1
(43,44) 1
(45,44) 1
(46,44) 1
(41,45) 1
(44,45) 1
(58,45) 1
(44,46) 1
(47,46) 1
(50,46) 1
(28,47) 1
(46,47) 1
(48,47) 1
(24,48) 1
(47,48) 1
(49,48) 1
(48,49) 1
(50,49) 1
(51,49) 1
(46,50) 1
(49,50) 1
(59,50) 1
(49,51) 1
(52,51) 1
(55,51) 1
(23,52) 1
(51,52) 1
(53,52) 1
(19,53) 1
(52,53) 1
(54,53) 1
(31,54) 1
(53,54) 1
(55,54) 1
(51,55) 1
(54,55) 1
(60,55) 1
(35,56) 1
(57,56) 1
(60,56) 1
(40,57) 1
(56,57) 1
(58,57) 1
(45,58) 1
(57,58) 1
(59,58) 1
(50,59) 1
(58,59) 1
(60,59) 1
(55,60) 1
(56,60) 1
(59,60) 1
其中1表示该坐标处有值!即将在该坐标留下标记
指定颜色和标记
B = bucky;
spy(B,'ro')
指定标记大小
B = bucky;
spy(B,'ro',2)
这里标记的指定与plot函数类似,r表示红色,o表示圆圈。后续,我们会综合讲解这些标记的应用!
最后,再给大家送上一个Matlab留下的狗头,这个大概是故意留下搞笑的!直接在Matlab命令行输入:spy,就可以得到下图:
转载文章,原文出处:MathWorks官网,由古哥整理发布
如若转载,请注明出处:https://iymark.com/articles/799.html