{"id":206,"date":"2009-11-19T21:34:57","date_gmt":"2009-11-19T21:34:57","guid":{"rendered":"https:\/\/blogs.mathworks.com\/loren\/2009\/11\/19\/coordinating-zero-removals-from-multiple-arrays\/"},"modified":"2018-01-08T15:24:28","modified_gmt":"2018-01-08T20:24:28","slug":"coordinating-zero-removals-from-multiple-arrays","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/loren\/2009\/11\/19\/coordinating-zero-removals-from-multiple-arrays\/","title":{"rendered":"Coordinating Zero Removals from Multiple Arrays"},"content":{"rendered":"<div xmlns:mwsh=\"https:\/\/www.mathworks.com\/namespace\/mcode\/v1\/syntaxhighlight.dtd\" class=\"content\">\r\n   <introduction>\r\n      <p>I've fielded some questions recently about how to coordinate multiple arrays changing simultaneously.  One example is removing\r\n         elements for two arrays in the case where either array holds a zero for the location. This is a good opportunity to reiterate\r\n         the use of logical arrays and some useful associated functions (such as <tt>any<\/tt> and <tt>all<\/tt>).\r\n      <\/p>\r\n   <\/introduction>\r\n   <h3>Contents<\/h3>\r\n   <div>\r\n      <ul>\r\n         <li><a href=\"#1\">Identify Pairs to Remove<\/a><\/li>\r\n         <li><a href=\"#3\">First Algorithm<\/a><\/li>\r\n         <li><a href=\"#5\">Second Algorithm<\/a><\/li>\r\n         <li><a href=\"#7\">Always Tradeoffs<\/a><\/li>\r\n      <\/ul>\r\n   <\/div>\r\n   <h3>Identify Pairs to Remove<a name=\"1\"><\/a><\/h3>\r\n   <p>Let's say I have 2 arrays<\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">a = [ 1  4  9  0 25  0 49  0]\r\nb = [ 1  0  3  0  0  6  7  8]<\/pre><pre style=\"font-style:oblique\">a =\r\n     1     4     9     0    25     0    49     0\r\nb =\r\n     1     0     3     0     0     6     7     8\r\n<\/pre><p>and I would like to delete the corresponding elements in <tt>a<\/tt> and <tt>b<\/tt> when either of them contains a zero value.\r\n   <\/p>\r\n   <h3>First Algorithm<a name=\"3\"><\/a><\/h3>\r\n   <p>There are several possible algorithms, each with their own trade-offs. Here's the first one.<\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">anyzero = any([a;b] == 0)\r\na(anyzero) = []\r\nb(anyzero) = []<\/pre><pre style=\"font-style:oblique\">anyzero =\r\n     0     1     0     1     1     1     0     1\r\na =\r\n     1     9    49\r\nb =\r\n     1     3     7\r\n<\/pre><p>This algorithm combines the two arrays into one, a potentially costly move if the arrays are large.  Then check for values\r\n      that equal zero.  And finally, check columnwise, using the function <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2009b\/techdoc\/ref\/any.html\"><tt>any<\/tt><\/a>, to identify the columns that have at least one zero.  Finally, use this array of logical indices to delete the appropriate\r\n      elements of <tt>a<\/tt> and <tt>b<\/tt>.\r\n   <\/p>\r\n   <h3>Second Algorithm<a name=\"5\"><\/a><\/h3>\r\n   <p>This algorithm (courtesy of Mirek L. in <a >this post<\/a> doesn't suffer from combining the two arrays.\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">x1 = a(a.*b ~= 0)\r\ny1 = b(a.*b ~= 0)<\/pre><pre style=\"font-style:oblique\">x1 =\r\n     1     9    49\r\ny1 =\r\n     1     3     7\r\n<\/pre><p>But it calculates the same temporary array twice (and it's the size of one of the vectors).  To be able to recalculate the\r\n      temporary array this way, I can't overwrite the initial arrays as you see in the first algorithm.  And finally, is there is\r\n      a <tt>NaN<\/tt> or <tt>Inf<\/tt> corresponding to a <tt>0<\/tt>, this algorithm won't find it.\r\n   <\/p>\r\n   <h3>Always Tradeoffs<a name=\"7\"><\/a><\/h3>\r\n   <p>There are always tradeoffs to make like the ones I mention here, at least when I program.  How do <b>you<\/b> choose which tradeoffs to make?  Which one would you choose here? Or would you choose an entirely different algorithm (which\r\n      I hope you'll post).  Let us know <a href=\"https:\/\/blogs.mathworks.com\/loren\/?p=206#respond\">here<\/a>.\r\n   <\/p><script language=\"JavaScript\">\r\n<!--\r\n\r\n    function grabCode_56a53b98f587445dab291e6d53aaed03() {\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='56a53b98f587445dab291e6d53aaed03 ' + '##### ' + 'SOURCE BEGIN' + ' #####';\r\n        t2='##### ' + 'SOURCE END' + ' #####' + ' 56a53b98f587445dab291e6d53aaed03';\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 = 'Loren Shure';\r\n        copyright = 'Copyright 2009 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_56a53b98f587445dab291e6d53aaed03()\"><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; 7.9<br><\/p>\r\n<\/div>\r\n<!--\r\n56a53b98f587445dab291e6d53aaed03 ##### SOURCE BEGIN #####\r\n%% Coordinating Zero Removals from Multiple Arrays\r\n% I've fielded some questions recently about how to coordinate multiple\r\n% arrays changing simultaneously.  One example is removing elements for two\r\n% arrays in the case where either array holds a zero for the location.\r\n% This is a good opportunity to reiterate the use of logical arrays and\r\n% some useful associated functions (such as |any| and |all|).\r\n\r\n\r\n%% Identify Pairs to Remove\r\n% Let's say I have 2 arrays\r\na = [ 1  4  9  0 25  0 49  0]\r\nb = [ 1  0  3  0  0  6  7  8]\r\n%%\r\n% and I would like to delete the corresponding elements in |a| and |b| when\r\n% either of them contains a zero value.\r\n%% First Algorithm\r\n% There are several possible algorithms, each with their own trade-offs.\r\n% Here's the first one.\r\nanyzero = any([a;b] == 0)\r\na(anyzero) = []\r\nb(anyzero) = []\r\n%%\r\n% This algorithm combines the two arrays into one, a potentially costly move\r\n% if the arrays are large.  Then check for values that equal zero.  And\r\n% finally, check columnwise, using the function \r\n% <https:\/\/www.mathworks.com\/help\/releases\/R2009b\/techdoc\/ref\/any.html |any|>,\r\n% to identify the columns that have at least one zero.  Finally, use this\r\n% array of logical indices to delete the appropriate elements of |a| and\r\n% |b|.\r\n%% Second Algorithm\r\n% This algorithm (courtesy of Mirek L. in\r\n% <http:\/\/view_thread\/263299 this post>\r\n% doesn't suffer from combining the two arrays. \r\nx1 = a(a.*b ~= 0)\r\ny1 = b(a.*b ~= 0)\r\n%%\r\n% But it calculates the same temporary array twice (and it's the size of\r\n% one of the vectors).  To be able to recalculate the temporary\r\n% array this way, I can't overwrite the initial arrays as you see in the\r\n% first algorithm.  And finally, is there is a |NaN| or |Inf| corresponding\r\n% to a |0|, this algorithm won't find it.\r\n%% Always Tradeoffs\r\n% There are always tradeoffs to make like the ones I mention here, at least\r\n% when I program.  How do *you* choose which tradeoffs to make?  Which one\r\n% would you choose here? Or would you choose an entirely different\r\n% algorithm (which I hope you'll post).  Let us know\r\n% <http:\/\/blogs.mathworks\/com\/loren\/?p=206#respond here>.\r\n\r\n##### SOURCE END ##### 56a53b98f587445dab291e6d53aaed03\r\n-->","protected":false},"excerpt":{"rendered":"<p>\r\n   \r\n      I've fielded some questions recently about how to coordinate multiple arrays changing simultaneously.  One example is removing\r\n         elements for two arrays in the case where either... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/loren\/2009\/11\/19\/coordinating-zero-removals-from-multiple-arrays\/\">read more >><\/a><\/p>","protected":false},"author":39,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[4,12],"tags":[],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/posts\/206"}],"collection":[{"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/users\/39"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/comments?post=206"}],"version-history":[{"count":2,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/posts\/206\/revisions"}],"predecessor-version":[{"id":2576,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/posts\/206\/revisions\/2576"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/media?parent=206"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/categories?post=206"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/tags?post=206"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}