<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
		>
<channel>
	<title>Comments on: Coordinating Zero Removals from Multiple Arrays</title>
	<atom:link href="http://blogs.mathworks.com/loren/2009/11/19/coordinating-zero-removals-from-multiple-arrays/feed/" rel="self" type="application/rss+xml" />
	<link>http://blogs.mathworks.com/loren/2009/11/19/coordinating-zero-removals-from-multiple-arrays/</link>
	<description>Loren Shure works on design of the MATLAB language at MathWorks. She writes here about once a week on MATLAB programming and related topics.</description>
	<lastBuildDate>Thu, 09 Feb 2012 04:19:21 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
	<item>
		<title>By: Arthur</title>
		<link>http://blogs.mathworks.com/loren/2009/11/19/coordinating-zero-removals-from-multiple-arrays/#comment-30899</link>
		<dc:creator>Arthur</dc:creator>
		<pubDate>Wed, 16 Dec 2009 23:40:11 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.mathworks.com/loren/2009/11/19/coordinating-zero-removals-from-multiple-arrays/#comment-30899</guid>
		<description>I spend a lot of time handling many-dimensioned matrices. Even if a concatenate based solution works in the N-dimensional case, I would still avoid a solution with any(matrix) because any()&#039;s behavior is not obvious for non-vectors. It&#039;s a pet peeve of mine that any, all, min, max, sum, and their relatives don&#039;t return a scalar by default. I would much rather any(X) return any(X(:)) instead of its current behavior, any(X,1). In that form, any(X) would actually answer the question &quot;is any element true?&quot; To me, that would make code written with any &amp; friends much more readable and dependable by avoiding unexpected matrix results.</description>
		<content:encoded><![CDATA[<p>I spend a lot of time handling many-dimensioned matrices. Even if a concatenate based solution works in the N-dimensional case, I would still avoid a solution with any(matrix) because any()&#8217;s behavior is not obvious for non-vectors. It&#8217;s a pet peeve of mine that any, all, min, max, sum, and their relatives don&#8217;t return a scalar by default. I would much rather any(X) return any(X(:)) instead of its current behavior, any(X,1). In that form, any(X) would actually answer the question &#8220;is any element true?&#8221; To me, that would make code written with any &amp; friends much more readable and dependable by avoiding unexpected matrix results.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Loren</title>
		<link>http://blogs.mathworks.com/loren/2009/11/19/coordinating-zero-removals-from-multiple-arrays/#comment-30841</link>
		<dc:creator>Loren</dc:creator>
		<pubDate>Fri, 20 Nov 2009 12:36:21 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.mathworks.com/loren/2009/11/19/coordinating-zero-removals-from-multiple-arrays/#comment-30841</guid>
		<description>Wow folks-

Always lots of interest when there&#039;s a quickie to try out!  I will only make 2 general comments so far.

1) why the math is faster than &amp; may have to do with the JIT and/or multi-threading - but I am not sure.
2) Solutions using find may be able to be sped up using just the logical expression instead as a logical index.  It&#039;s being computed in the find expression already, and doesn&#039;t have to translate to locations and then index back in.

--Loren</description>
		<content:encoded><![CDATA[<p>Wow folks-</p>
<p>Always lots of interest when there&#8217;s a quickie to try out!  I will only make 2 general comments so far.</p>
<p>1) why the math is faster than &#038; may have to do with the JIT and/or multi-threading &#8211; but I am not sure.<br />
2) Solutions using find may be able to be sped up using just the logical expression instead as a logical index.  It&#8217;s being computed in the find expression already, and doesn&#8217;t have to translate to locations and then index back in.</p>
<p>&#8211;Loren</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Dan</title>
		<link>http://blogs.mathworks.com/loren/2009/11/19/coordinating-zero-removals-from-multiple-arrays/#comment-30839</link>
		<dc:creator>Dan</dc:creator>
		<pubDate>Fri, 20 Nov 2009 11:30:40 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.mathworks.com/loren/2009/11/19/coordinating-zero-removals-from-multiple-arrays/#comment-30839</guid>
		<description>I like the first way better than the second way. Combining the arrays into one and running any is nice, although as you mention it could be costly. I would do this:
&lt;pre&gt;
a = [ 1  4  9  0 25  0 49  0];
b = [ 1  0  3  0  0  6  7  8];
anyzero = unique([find(a == 0), find(b == 0)]);
a(anyzero) = []
b(anyzero) = []
&lt;/pre&gt;
As with most of my code, it may not work, but it seems to match your answer. It also calls find twice, which may be costly. Finally, I am not sure if unique is better/worse than any.</description>
		<content:encoded><![CDATA[<p>I like the first way better than the second way. Combining the arrays into one and running any is nice, although as you mention it could be costly. I would do this:</p>
<pre>
a = [ 1  4  9  0 25  0 49  0];
b = [ 1  0  3  0  0  6  7  8];
anyzero = unique([find(a == 0), find(b == 0)]);
a(anyzero) = []
b(anyzero) = []
</pre>
<p>As with most of my code, it may not work, but it seems to match your answer. It also calls find twice, which may be costly. Finally, I am not sure if unique is better/worse than any.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: James Myatt</title>
		<link>http://blogs.mathworks.com/loren/2009/11/19/coordinating-zero-removals-from-multiple-arrays/#comment-30838</link>
		<dc:creator>James Myatt</dc:creator>
		<pubDate>Fri, 20 Nov 2009 11:25:47 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.mathworks.com/loren/2009/11/19/coordinating-zero-removals-from-multiple-arrays/#comment-30838</guid>
		<description>How about

&lt;pre&gt;
I = (a == 0 &#124; b == 0);
a(I) = [];
b(I) = [];
&lt;/pre&gt;</description>
		<content:encoded><![CDATA[<p>How about</p>
<pre>
I = (a == 0 | b == 0);
a(I) = [];
b(I) = [];
</pre>
]]></content:encoded>
	</item>
	<item>
		<title>By: Tunc</title>
		<link>http://blogs.mathworks.com/loren/2009/11/19/coordinating-zero-removals-from-multiple-arrays/#comment-30837</link>
		<dc:creator>Tunc</dc:creator>
		<pubDate>Fri, 20 Nov 2009 10:38:06 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.mathworks.com/loren/2009/11/19/coordinating-zero-removals-from-multiple-arrays/#comment-30837</guid>
		<description>Hello Loren,

love your blog because of such inspiring and challenging comments to such &#039;small&#039; programming questions. My quick &amp; dirty approach which did the job for me: 

&lt;pre&gt;
a(find(a~=0 &amp; b~=0))
b(find(a~=0 &amp; b~=0))
&lt;/pre&gt;</description>
		<content:encoded><![CDATA[<p>Hello Loren,</p>
<p>love your blog because of such inspiring and challenging comments to such &#8216;small&#8217; programming questions. My quick &amp; dirty approach which did the job for me: </p>
<pre>
a(find(a~=0 &amp; b~=0))
b(find(a~=0 &amp; b~=0))
</pre>
]]></content:encoded>
	</item>
	<item>
		<title>By: Pekka Kumpulainen</title>
		<link>http://blogs.mathworks.com/loren/2009/11/19/coordinating-zero-removals-from-multiple-arrays/#comment-30836</link>
		<dc:creator>Pekka Kumpulainen</dc:creator>
		<pubDate>Fri, 20 Nov 2009 08:04:28 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.mathworks.com/loren/2009/11/19/coordinating-zero-removals-from-multiple-arrays/#comment-30836</guid>
		<description>Here is my tradeoff. 
I usually want to keep the original variables as they are most probably needed as such later on. So instead of cutting the variables by assigning empty, I would invert the logic.

a_z = a~=0;
b_z = b~=0;
Two logical arrays, which need less memory than temporarily combining a and b
Third logical
ind = a_z&#124;b_z;
or
ind = a_z&amp;b_z;
depending on whether you want any or all zeros.
New variables
na = a(ind); nb = = b(ind);
This avoids combining the a and b as in the first example. And also the multiplication that was used in the second.
If you want to overwrite the original values, just negate the logical operators used here and assign
a(ind) = [];
The variables will still be moved to a new location in memory, right? As their size will change we can&#039;t use the (1:end) index trick to keep them in the same location.</description>
		<content:encoded><![CDATA[<p>Here is my tradeoff.<br />
I usually want to keep the original variables as they are most probably needed as such later on. So instead of cutting the variables by assigning empty, I would invert the logic.</p>
<p>a_z = a~=0;<br />
b_z = b~=0;<br />
Two logical arrays, which need less memory than temporarily combining a and b<br />
Third logical<br />
ind = a_z|b_z;<br />
or<br />
ind = a_z&amp;b_z;<br />
depending on whether you want any or all zeros.<br />
New variables<br />
na = a(ind); nb = = b(ind);<br />
This avoids combining the a and b as in the first example. And also the multiplication that was used in the second.<br />
If you want to overwrite the original values, just negate the logical operators used here and assign<br />
a(ind) = [];<br />
The variables will still be moved to a new location in memory, right? As their size will change we can&#8217;t use the (1:end) index trick to keep them in the same location.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Iain</title>
		<link>http://blogs.mathworks.com/loren/2009/11/19/coordinating-zero-removals-from-multiple-arrays/#comment-30835</link>
		<dc:creator>Iain</dc:creator>
		<pubDate>Fri, 20 Nov 2009 06:11:17 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.mathworks.com/loren/2009/11/19/coordinating-zero-removals-from-multiple-arrays/#comment-30835</guid>
		<description>Followup:

Of course, to allow NaNs (counting them as non-zero):
&lt;pre&gt;mask = (a~=0) &amp; (b~=0);&lt;/pre&gt;
The mask says “a and b should be non-zero”.

If, on reflection, NaNs should veto inclusion as well as zeros, that’s easy too:
&lt;pre&gt;mask = ~((a==0) &#124; isnan(a) &#124; (b==0) &#124; isnan(b));&lt;pre&gt;</description>
		<content:encoded><![CDATA[<p>Followup:</p>
<p>Of course, to allow NaNs (counting them as non-zero):</p>
<pre>mask = (a~=0) &amp; (b~=0);</pre>
<p>The mask says “a and b should be non-zero”.</p>
<p>If, on reflection, NaNs should veto inclusion as well as zeros, that’s easy too:</p>
<pre>mask = ~((a==0) | isnan(a) | (b==0) | isnan(b));
</pre><pre>
</pre>]]></content:encoded>
	</item>
	<item>
		<title>By: Matt Fig</title>
		<link>http://blogs.mathworks.com/loren/2009/11/19/coordinating-zero-removals-from-multiple-arrays/#comment-30834</link>
		<dc:creator>Matt Fig</dc:creator>
		<pubDate>Fri, 20 Nov 2009 04:50:24 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.mathworks.com/loren/2009/11/19/coordinating-zero-removals-from-multiple-arrays/#comment-30834</guid>
		<description>I would usually go with something like this:

&lt;pre&gt; &lt;code&gt;
y = a&amp;b;
x = a(y);
y = b(y);
&lt;/code&gt; &lt;/pre&gt;

But I was surprised to find that the second solution you posted was faster, even with the multiplication and then logical comparison all happening twice!  Why is that?
I thought maybe it was because the memory for the index wasn&#039;t specifically stored, but this is even slower (as I would have expected):

&lt;pre&gt; &lt;code&gt;
x2 = a(a&amp;b);
y2 = b(a&amp;b);
&lt;/code&gt; &lt;/pre&gt;

So how can a simple &amp; be slower than element by element multiplication then a logical comparison?
I used a = round(rand(10000,1)*3); and similar for b.</description>
		<content:encoded><![CDATA[<p>I would usually go with something like this:</p>
<pre> <code>
y = a&#038;b;
x = a(y);
y = b(y);
</code> </pre>
<p>But I was surprised to find that the second solution you posted was faster, even with the multiplication and then logical comparison all happening twice!  Why is that?<br />
I thought maybe it was because the memory for the index wasn&#8217;t specifically stored, but this is even slower (as I would have expected):</p>
<pre> <code>
x2 = a(a&amp;b);
y2 = b(a&amp;b);
</code> </pre>
<p>So how can a simple &amp; be slower than element by element multiplication then a logical comparison?<br />
I used a = round(rand(10000,1)*3); and similar for b.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: kk</title>
		<link>http://blogs.mathworks.com/loren/2009/11/19/coordinating-zero-removals-from-multiple-arrays/#comment-30833</link>
		<dc:creator>kk</dc:creator>
		<pubDate>Fri, 20 Nov 2009 03:16:29 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.mathworks.com/loren/2009/11/19/coordinating-zero-removals-from-multiple-arrays/#comment-30833</guid>
		<description>c=all([a;b])
a(c)
a(b)</description>
		<content:encoded><![CDATA[<p>c=all([a;b])<br />
a(c)<br />
a(b)</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Iain</title>
		<link>http://blogs.mathworks.com/loren/2009/11/19/coordinating-zero-removals-from-multiple-arrays/#comment-30831</link>
		<dc:creator>Iain</dc:creator>
		<pubDate>Fri, 20 Nov 2009 01:46:41 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.mathworks.com/loren/2009/11/19/coordinating-zero-removals-from-multiple-arrays/#comment-30831</guid>
		<description>My knee-jerk response was:
&lt;pre&gt;
mask = a &amp; b;
a = a(mask);
b = b(mask);
&lt;/pre&gt;
This is clearer to me as I don&#039;t have the exact semantics of any() in my head. It deals with Infs ok, but fails on NaNs, which can&#039;t be converted to logicals.</description>
		<content:encoded><![CDATA[<p>My knee-jerk response was:</p>
<pre>
mask = a &amp; b;
a = a(mask);
b = b(mask);
</pre>
<p>This is clearer to me as I don&#8217;t have the exact semantics of any() in my head. It deals with Infs ok, but fails on NaNs, which can&#8217;t be converted to logicals.</p>
]]></content:encoded>
	</item>
</channel>
</rss>

