<?xml version="1.0" encoding="UTF-8"?><!-- generator="wordpress/2.3.1" -->
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	>
<channel>
	<title>Comments for Loren on the Art of MATLAB</title>
	<link>http://blogs.mathworks.com/loren</link>
	<description>Loren Shure  works on design of the MATLAB language at &#60;a href="http://www.mathworks.com/"&#62;The MathWorks&#60;/a&#62;. She writes here about once a week on MATLAB programming and related topics. &#60;br&#62;&#60;br&#62;&#60;a href="/images/loren-full.jpg"&#62;&#60;img src="/images/loren.jpg"&#62;&#60;/a&#62;</description>
	<pubDate>Sat, 21 Nov 2009 01:57:14 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.3.1</generator>
		<item>
		<title>Comment on Dealing with Cells by Jun</title>
		<link>http://blogs.mathworks.com/loren/2009/10/21/dealing-with-cells/#comment-30842</link>
		<dc:creator>Jun</dc:creator>
		<pubDate>Fri, 20 Nov 2009 14:28:05 +0000</pubDate>
		<guid>http://blogs.mathworks.com/loren/2009/10/21/dealing-with-cells/#comment-30842</guid>
		<description>&lt;pre&gt;
I totally can not believe it, Loren. You are really helpful. Thank you so much, MATLAB master!
&lt;/pre&gt;</description>
		<content:encoded><![CDATA[<pre>
I totally can not believe it, Loren. You are really helpful. Thank you so much, MATLAB master!
</pre>
]]></content:encoded>
	</item>
	<item>
		<title>Comment on Coordinating Zero Removals from Multiple Arrays 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>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's a quickie to try out!  I will only make 2 general comments so far.

1) why the math is faster than &#038; 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's being computed in the find expression already, and doesn'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 - 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>Comment on Dealing with Cells by Loren</title>
		<link>http://blogs.mathworks.com/loren/2009/10/21/dealing-with-cells/#comment-30840</link>
		<dc:creator>Loren</dc:creator>
		<pubDate>Fri, 20 Nov 2009 12:32:08 +0000</pubDate>
		<guid>http://blogs.mathworks.com/loren/2009/10/21/dealing-with-cells/#comment-30840</guid>
		<description>Jun-

ismember is your friend here:

&lt;pre class="code"&gt;
&gt;&gt; [aa,ind] = ismember(Array2,Array1)
aa =
     1     1     1     1     1     1     1
ind =
     1     2     1     4     4     3     2
&lt;/pre&gt;

--Loren</description>
		<content:encoded><![CDATA[<p>Jun-</p>
<p>ismember is your friend here:</p>
<pre class="code">
>> [aa,ind] = ismember(Array2,Array1)
aa =
     1     1     1     1     1     1     1
ind =
     1     2     1     4     4     3     2
</pre>
<p>&#8211;Loren</p>
]]></content:encoded>
	</item>
	<item>
		<title>Comment on Coordinating Zero Removals from Multiple Arrays 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>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>Comment on Coordinating Zero Removals from Multiple Arrays 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>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>Comment on Coordinating Zero Removals from Multiple Arrays 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>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 'small' programming questions. My quick &#38; dirty approach which did the job for me: 

&lt;pre&gt;
a(find(a~=0 &#38; b~=0))
b(find(a~=0 &#38; 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 &#8217;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>Comment on Coordinating Zero Removals from Multiple Arrays 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>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&#38;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'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>Comment on Coordinating Zero Removals from Multiple Arrays 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>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) &#38; (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>Comment on Coordinating Zero Removals from Multiple Arrays 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>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&#038;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't specifically stored, but this is even slower (as I would have expected):

&lt;pre&gt; &lt;code&gt;
x2 = a(a&#38;b);
y2 = b(a&#38;b);
&lt;/code&gt; &lt;/pre&gt;

So how can a simple &#38; 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>Comment on Coordinating Zero Removals from Multiple Arrays 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>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>
</channel>
</rss>
