{"id":5382,"date":"2014-06-13T09:00:52","date_gmt":"2014-06-13T13:00:52","guid":{"rendered":"https:\/\/blogs.mathworks.com\/pick\/?p=5382"},"modified":"2014-06-10T22:59:16","modified_gmt":"2014-06-11T02:59:16","slug":"spy-with-color","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/pick\/2014\/06\/13\/spy-with-color\/","title":{"rendered":"Spy with color"},"content":{"rendered":"<div xmlns:mwsh=\"https:\/\/www.mathworks.com\/namespace\/mcode\/v1\/syntaxhighlight.dtd\" class=\"content\">\r\n   <p><a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/authors\/15007\">Jiro<\/a>'s pick this week is <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/46551-cspy-m\"><tt>cspy<\/tt><\/a> by <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/authors\/464315\">Hugo Gualdron<\/a>.\r\n   <\/p>\r\n   <p><a href=\"https:\/\/www.mathworks.com\/help\/matlab\/ref\/spy.html\"><tt>spy<\/tt><\/a> allows you to visualize sparse matrices:\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">spy(bucky)<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/pick\/jiro\/potw_cspy\/potw_cspy_01.png\"> <p>(Anyone know how to get the Easter egg for the <tt>spy<\/tt> function?)\r\n   <\/p>\r\n   <p><tt>spy<\/tt> is meant for displaying the positions of the nonzero elements in the sparse matrix. But if the sparse matrix contains multiple\r\n      values (levels), it would be nice to visualize those levels as well. That's where <tt>cspy<\/tt> comes in. Take the following example:\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">BW = logical ([<span style=\"color: #0000FF\">...<\/span>\r\n    0     0     0     0     0     0     0     0\r\n    0     0     0     0     1     1     0     0\r\n    0     0     0     0     1     0     0     0\r\n    0     1     1     0     0     0     1     0\r\n    0     1     1     1     0     0     1     0\r\n    0     1     1     0     0     0     1     0\r\n    0     1     0     0     0     1     1     0\r\n    0     0     0     0     0     0     0     0]);\r\nimshow(BW,<span style=\"color: #A020F0\">'InitialMagnification'<\/span>,<span style=\"color: #A020F0\">'fit'<\/span>)<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/pick\/jiro\/potw_cspy\/potw_cspy_02.png\"> <p>In the above black and white image, we can say that there are 3 white blobs. We can use the <a href=\"https:\/\/www.mathworks.com\/help\/images\/ref\/bwlabel.html\"><tt>bwlabel<\/tt><\/a> function from the <a href=\"https:\/\/www.mathworks.com\/products\/image\/\">Image Processing Toolbox<\/a> to identify the 3 regions and mark them with unique identifiers.\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">L = bwlabel(BW)<\/pre><pre style=\"font-style:oblique\">L =\r\n     0     0     0     0     0     0     0     0\r\n     0     0     0     0     2     2     0     0\r\n     0     0     0     0     2     0     0     0\r\n     0     1     1     0     0     0     3     0\r\n     0     1     1     1     0     0     3     0\r\n     0     1     1     0     0     0     3     0\r\n     0     1     0     0     0     3     3     0\r\n     0     0     0     0     0     0     0     0\r\n<\/pre><p>You could imagine that this matrix <tt>L<\/tt> can be a highly sparse matrix with far more zeros than non-zero elements. Let's store this as a sparse matrix.\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">Ls = sparse(L)<\/pre><pre style=\"font-style:oblique\">Ls =\r\n   (4,2)        1\r\n   (5,2)        1\r\n   (6,2)        1\r\n   (7,2)        1\r\n   (4,3)        1\r\n   (5,3)        1\r\n   (6,3)        1\r\n   (5,4)        1\r\n   (2,5)        2\r\n   (3,5)        2\r\n   (2,6)        2\r\n   (7,6)        3\r\n   (4,7)        3\r\n   (5,7)        3\r\n   (6,7)        3\r\n   (7,7)        3\r\n<\/pre><p>Even for this small example, you can see some memory savings.<\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">whos <span style=\"color: #A020F0\">L<\/span> <span style=\"color: #A020F0\">Ls<\/span><\/pre><pre style=\"font-style:oblique\">  Name      Size            Bytes  Class     Attributes\r\n\r\n  L         8x8               512  double              \r\n  Ls        8x8               328  double    sparse    \r\n\r\n<\/pre><p>Now, we can use <tt>cspy<\/tt> to visualize <tt>Ls<\/tt> with different colors for the different values.\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">figure\r\ncspy(Ls)<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/pick\/jiro\/potw_cspy\/potw_cspy_03.png\"> <p>Wonderful!<\/p>\r\n   <p>In addition to this useful functionality, I really like the great help that's included. The function has a number of optional\r\n      parameters to customize the plot, and Hugo explains each syntax in detail. He also includes a number of examples.\r\n   <\/p>\r\n   <p><b>Comments<\/b><\/p>\r\n   <p>Do you work with sparse matrices? Do you have any custom visualizations for them? Let us know about them <a href=\"https:\/\/blogs.mathworks.com\/pick\/?p=5382#respond\">here<\/a>. Or give <tt>cspy<\/tt> a try and leave a <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/46551-cspy-m#comments\">comment<\/a> for Hugo.\r\n   <\/p><script language=\"JavaScript\">\r\n<!--\r\n\r\n    function grabCode_f56b1972ebd446908598a6a13b353d1b() {\r\n        \/\/ Remember the title so we can use it in the new page\r\n        title = document.title;\r\n\r\n        \/\/ Break up these strings so that their presence\r\n        \/\/ in the Javascript doesn't mess up the search for\r\n        \/\/ the MATLAB code.\r\n        t1='f56b1972ebd446908598a6a13b353d1b ' + '##### ' + 'SOURCE BEGIN' + ' #####';\r\n        t2='##### ' + 'SOURCE END' + ' #####' + ' f56b1972ebd446908598a6a13b353d1b';\r\n    \r\n        b=document.getElementsByTagName('body')[0];\r\n        i1=b.innerHTML.indexOf(t1)+t1.length;\r\n        i2=b.innerHTML.indexOf(t2);\r\n \r\n        code_string = b.innerHTML.substring(i1, i2);\r\n        code_string = code_string.replace(\/REPLACE_WITH_DASH_DASH\/g,'--');\r\n\r\n        \/\/ Use \/x3C\/g instead of the less-than character to avoid errors \r\n        \/\/ in the XML parser.\r\n        \/\/ Use '\\x26#60;' instead of '<' so that the XML parser\r\n        \/\/ doesn't go ahead and substitute the less-than character. \r\n        code_string = code_string.replace(\/\\x3C\/g, '\\x26#60;');\r\n\r\n        author = '';\r\n        copyright = 'Copyright 2014 The MathWorks, Inc.';\r\n\r\n        w = window.open();\r\n        d = w.document;\r\n        d.write('<pre>\\n');\r\n        d.write(code_string);\r\n\r\n        \/\/ Add author and copyright lines at the bottom if specified.\r\n        if ((author.length > 0) || (copyright.length > 0)) {\r\n            d.writeln('');\r\n            d.writeln('%%');\r\n            if (author.length > 0) {\r\n                d.writeln('% _' + author + '_');\r\n            }\r\n            if (copyright.length > 0) {\r\n                d.writeln('% _' + copyright + '_');\r\n            }\r\n        }\r\n\r\n        d.write('<\/pre>\\n');\r\n      \r\n      d.title = title + ' (MATLAB code)';\r\n      d.close();\r\n      }   \r\n      \r\n-->\r\n<\/script><p style=\"text-align: right; font-size: xx-small; font-weight:lighter;   font-style: italic; color: gray\"><br><a href=\"javascript:grabCode_f56b1972ebd446908598a6a13b353d1b()\"><span style=\"font-size: x-small;        font-style: italic;\">Get \r\n            the MATLAB code \r\n            <noscript>(requires JavaScript)<\/noscript><\/span><\/a><br><br>\r\n      Published with MATLAB&reg; R2014a<br><\/p>\r\n<\/div>\r\n<!--\r\nf56b1972ebd446908598a6a13b353d1b ##### SOURCE BEGIN #####\r\n%%\r\n% <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/authors\/15007\r\n% Jiro>'s pick this week is\r\n% <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/46551-cspy-m |cspy|> by\r\n% <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/authors\/464315 Hugo\r\n% Gualdron>.\r\n%\r\n% <https:\/\/www.mathworks.com\/help\/matlab\/ref\/spy.html |spy|> allows you to\r\n% visualize sparse matrices:\r\n\r\nspy(bucky)\r\n\r\n%%\r\n% (Anyone know how to get the Easter egg for the |spy| function?)\r\n%\r\n% |spy| is meant for displaying the positions of the nonzero elements in\r\n% the sparse matrix. But if the sparse matrix contains multiple values\r\n% (levels), it would be nice to visualize those levels as well. That's\r\n% where |cspy| comes in. Take the following example:\r\n\r\nBW = logical ([...\r\n    0     0     0     0     0     0     0     0\r\n    0     0     0     0     1     1     0     0\r\n    0     0     0     0     1     0     0     0\r\n    0     1     1     0     0     0     1     0\r\n    0     1     1     1     0     0     1     0\r\n    0     1     1     0     0     0     1     0\r\n    0     1     0     0     0     1     1     0\r\n    0     0     0     0     0     0     0     0]);\r\nimshow(BW,'InitialMagnification','fit')\r\n\r\n%%\r\n% In the above black and white image, we can say that there are 3 white\r\n% blobs. We can use the\r\n% <https:\/\/www.mathworks.com\/help\/images\/ref\/bwlabel.html |bwlabel|>\r\n% function from the <https:\/\/www.mathworks.com\/products\/image\/ Image\r\n% Processing Toolbox> to identify the 3 regions and mark them with unique\r\n% identifiers.\r\n\r\nL = bwlabel(BW)\r\n\r\n%%\r\n% You could imagine that this matrix |L| can be a highly sparse matrix with\r\n% far more zeros than non-zero elements. Let's store this as a sparse\r\n% matrix.\r\n\r\nLs = sparse(L)\r\n\r\n%%\r\n% Even for this small example, you can see some memory savings.\r\n\r\nwhos L Ls\r\n\r\n%%\r\n% Now, we can use |cspy| to visualize |Ls| with different colors for the\r\n% different values.\r\n\r\nfigure\r\ncspy(Ls)\r\n\r\n%%\r\n% Wonderful!\r\n%\r\n% In addition to this useful functionality, I really like the great help\r\n% that's included. The function has a number of optional parameters to\r\n% customize the plot, and Hugo explains each syntax in detail. He also\r\n% includes a number of examples.\r\n%\r\n% *Comments*\r\n%\r\n% Do you work with sparse matrices? Do you have any custom visualizations\r\n% for them? Let us know about them\r\n% <https:\/\/blogs.mathworks.com\/pick\/?p=5382#respond here>. Or give |cspy| a\r\n% try and leave a\r\n% <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/46551-cspy-m#comments\r\n% comment> for Hugo.\r\n\r\n##### SOURCE END ##### f56b1972ebd446908598a6a13b353d1b\r\n-->","protected":false},"excerpt":{"rendered":"<div class=\"overview-image\"><img decoding=\"async\"  class=\"img-responsive\" src=\"https:\/\/blogs.mathworks.com\/images\/pick\/jiro\/potw_cspy\/potw_cspy_01.png\" onError=\"this.style.display ='none';\" \/><\/div><p>\r\n   Jiro's pick this week is cspy by Hugo Gualdron.\r\n   \r\n   spy allows you to visualize sparse matrices:\r\n   spy(bucky) (Anyone know how to get the Easter egg for the spy function?)\r\n   \r\n   spy is... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/pick\/2014\/06\/13\/spy-with-color\/\">read more >><\/a><\/p>","protected":false},"author":35,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[16],"tags":[],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts\/5382"}],"collection":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/users\/35"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/comments?post=5382"}],"version-history":[{"count":3,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts\/5382\/revisions"}],"predecessor-version":[{"id":5385,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts\/5382\/revisions\/5385"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/media?parent=5382"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/categories?post=5382"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/tags?post=5382"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}