<?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: Calculus with Empty Arrays</title>
	<atom:link href="http://blogs.mathworks.com/loren/2009/11/04/calculus-with-empty-arrays/feed/" rel="self" type="application/rss+xml" />
	<link>http://blogs.mathworks.com/loren/2009/11/04/calculus-with-empty-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: Loren</title>
		<link>http://blogs.mathworks.com/loren/2009/11/04/calculus-with-empty-arrays/#comment-31382</link>
		<dc:creator>Loren</dc:creator>
		<pubDate>Tue, 25 May 2010 16:35:15 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.mathworks.com/loren/2009/11/04/calculus-with-empty-arrays/#comment-31382</guid>
		<description>Ben-

This is intentional to deal with backward compatibility details from before the function cat existed.  cat is strictly enforcing the dimension matching while [], which uses horzcat and vertcat, used to have to deal with 0x0 empties, and then some empties arose from deleting elements in an array and become 0x1 or 1x0.  To have people&#039;s code still work, you see the result you noted.

--Loren</description>
		<content:encoded><![CDATA[<p>Ben-</p>
<p>This is intentional to deal with backward compatibility details from before the function cat existed.  cat is strictly enforcing the dimension matching while [], which uses horzcat and vertcat, used to have to deal with 0&#215;0 empties, and then some empties arose from deleting elements in an array and become 0&#215;1 or 1&#215;0.  To have people&#8217;s code still work, you see the result you noted.</p>
<p>&#8211;Loren</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Ben Abbott</title>
		<link>http://blogs.mathworks.com/loren/2009/11/04/calculus-with-empty-arrays/#comment-31376</link>
		<dc:creator>Ben Abbott</dc:creator>
		<pubDate>Sun, 23 May 2010 14:57:28 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.mathworks.com/loren/2009/11/04/calculus-with-empty-arrays/#comment-31376</guid>
		<description>Loren,

I&#039;m a late arrival to this thread, but discovered an unexpected result when concatenating matrices and thought I&#039;d ask about it.

&lt;pre&gt;
&gt;&gt; cat (1, zeros(1,0), 1)
??? Error using ==&gt; cat
CAT arguments dimensions are not consistent.
 
&gt;&gt; [zeros(1,0); 1]

ans =

     1
&lt;/pre&gt;

Is this a bug, a feature, or by design?</description>
		<content:encoded><![CDATA[<p>Loren,</p>
<p>I&#8217;m a late arrival to this thread, but discovered an unexpected result when concatenating matrices and thought I&#8217;d ask about it.</p>
<pre>
&gt;&gt; cat (1, zeros(1,0), 1)
??? Error using ==&gt; cat
CAT arguments dimensions are not consistent.

&gt;&gt; [zeros(1,0); 1]

ans =

     1
</pre>
<p>Is this a bug, a feature, or by design?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: luiz otávio</title>
		<link>http://blogs.mathworks.com/loren/2009/11/04/calculus-with-empty-arrays/#comment-30768</link>
		<dc:creator>luiz otávio</dc:creator>
		<pubDate>Thu, 12 Nov 2009 01:43:34 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.mathworks.com/loren/2009/11/04/calculus-with-empty-arrays/#comment-30768</guid>
		<description>thanks for your help.</description>
		<content:encoded><![CDATA[<p>thanks for your help.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Loren</title>
		<link>http://blogs.mathworks.com/loren/2009/11/04/calculus-with-empty-arrays/#comment-30764</link>
		<dc:creator>Loren</dc:creator>
		<pubDate>Wed, 11 Nov 2009 12:14:47 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.mathworks.com/loren/2009/11/04/calculus-with-empty-arrays/#comment-30764</guid>
		<description>Luis-

Your code is very close.  All you need to do is initialize resultado to i (=8) before the while loop instead of to the empty array.

&lt;pre class=&#039;code&#039;&gt;
i=8;
resultado=i;
while (i&gt;1)
i=i-1;
resultado=[resultado i]
end
&lt;/pre&gt;

There are other ways as well, for example, updating resultado in the loop before decrementing i.  Then the loop would need to be i&gt;=1 as well, I think. (Or i&gt;0.)

--Loren</description>
		<content:encoded><![CDATA[<p>Luis-</p>
<p>Your code is very close.  All you need to do is initialize resultado to i (=8) before the while loop instead of to the empty array.</p>
<pre class='code'>
i=8;
resultado=i;
while (i>1)
i=i-1;
resultado=[resultado i]
end
</pre>
<p>There are other ways as well, for example, updating resultado in the loop before decrementing i.  Then the loop would need to be i>=1 as well, I think. (Or i>0.)</p>
<p>&#8211;Loren</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: luiz otávio</title>
		<link>http://blogs.mathworks.com/loren/2009/11/04/calculus-with-empty-arrays/#comment-30762</link>
		<dc:creator>luiz otávio</dc:creator>
		<pubDate>Wed, 11 Nov 2009 06:24:37 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.mathworks.com/loren/2009/11/04/calculus-with-empty-arrays/#comment-30762</guid>
		<description>Hi! How are you?

I need your help,I&#039;m trying to do a countdown as in the code:

i=8;
resultado=[];
while (i&gt;1)
    i=i-1;
    resultado=[resultado i]
end

This code shows the response: [7 6 5 4 3 2 1]
but I wish he showed: [8 7 6 5 4 3 2 1] 	
ie the input value will appear as the first element of the response.

Please be very grateful if you can help and thanks for your time.</description>
		<content:encoded><![CDATA[<p>Hi! How are you?</p>
<p>I need your help,I&#8217;m trying to do a countdown as in the code:</p>
<p>i=8;<br />
resultado=[];<br />
while (i&gt;1)<br />
    i=i-1;<br />
    resultado=[resultado i]<br />
end</p>
<p>This code shows the response: [7 6 5 4 3 2 1]<br />
but I wish he showed: [8 7 6 5 4 3 2 1]<br />
ie the input value will appear as the first element of the response.</p>
<p>Please be very grateful if you can help and thanks for your time.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Loren</title>
		<link>http://blogs.mathworks.com/loren/2009/11/04/calculus-with-empty-arrays/#comment-30761</link>
		<dc:creator>Loren</dc:creator>
		<pubDate>Tue, 10 Nov 2009 22:45:46 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.mathworks.com/loren/2009/11/04/calculus-with-empty-arrays/#comment-30761</guid>
		<description>Paul-

MATLAB always evaluates the RHS before looking to see if it can assign into the LHS, as far as I know (and according to what you found out also).  Therefore MATLAB will not do the kind of short-circuiting to not evaluate the RHS, even if it&#039;s ultimately irrelevant for the LHS assignment.

--Loren</description>
		<content:encoded><![CDATA[<p>Paul-</p>
<p>MATLAB always evaluates the RHS before looking to see if it can assign into the LHS, as far as I know (and according to what you found out also).  Therefore MATLAB will not do the kind of short-circuiting to not evaluate the RHS, even if it&#8217;s ultimately irrelevant for the LHS assignment.</p>
<p>&#8211;Loren</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Paul Jackson</title>
		<link>http://blogs.mathworks.com/loren/2009/11/04/calculus-with-empty-arrays/#comment-30760</link>
		<dc:creator>Paul Jackson</dc:creator>
		<pubDate>Tue, 10 Nov 2009 22:27:14 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.mathworks.com/loren/2009/11/04/calculus-with-empty-arrays/#comment-30760</guid>
		<description>Loren,

1. In your response (14) to my questions, you said &quot;Now let’s look at the index on the right hand side. If it’s empty, and the LHS is not, you get an error ...&quot;  Did you really mean &quot;and the index on the LHS is not...?&quot;  This seems to be true, but I might be a little surprised about this:
&gt;&gt; x=1:5;y=1:5;
&gt;&gt; x(zeros(1,0))
ans =
   Empty matrix: 1-by-0
&gt;&gt; y(zeros(1,0,1,0))
ans =
   Empty array: 1-by-0-by-1-by-0
&gt;&gt;   % assignment with incompatible dimensions?
&gt;&gt; x(zeros(1,0))=y(zeros(1,0,1,0))
x =
     1     2     3     4     5

2. The source of my question in general goes back to the dark ages when Matlab didn&#039;t have logical indexing and we would write code like this because we weren&#039;t quite sure what to make of the assignment statement with an empty index.

ii=find(x&gt;100);
if ~isempty(ii)
  y(ii) = rhs
end

Even now, we sometimes write:
ii = x&gt;100;
if any(ii),
   y(ii) = rhs;
end

This code was the motivation for my reference to &quot;empty logicals&quot; when I really meant a logical vector that&#039;s all false.  I guess in this case ii has to have dimensions compatible with y and rhs and then y(ii) = rhs is basically a no-op (unless evaluation of rhs has side effects)?

3.  Which brings me to the question about the size of the matrices involved.  Using the if(any) construction above potentially removes the need to compute the rhs, which could be expensive.  My question was really if Matlab sees the expression x(ii) = rhs and sees that ii is empty (or all false), will it short circuit the assignment and not even bother to evaluate the rhs.  From what you described above, and what I see from some quick experiments, is that the short circuit does not occur because Matlab still checks that the assignment follows the rules for dimensions, and I guess it doesn&#039;t know about potential side effects of evaluating rhs.  I suppose this makes sense and it&#039;s easy enough to use the if(any) if wanted.</description>
		<content:encoded><![CDATA[<p>Loren,</p>
<p>1. In your response (14) to my questions, you said &#8220;Now let’s look at the index on the right hand side. If it’s empty, and the LHS is not, you get an error &#8230;&#8221;  Did you really mean &#8220;and the index on the LHS is not&#8230;?&#8221;  This seems to be true, but I might be a little surprised about this:<br />
&gt;&gt; x=1:5;y=1:5;<br />
&gt;&gt; x(zeros(1,0))<br />
ans =<br />
   Empty matrix: 1-by-0<br />
&gt;&gt; y(zeros(1,0,1,0))<br />
ans =<br />
   Empty array: 1-by-0-by-1-by-0<br />
&gt;&gt;   % assignment with incompatible dimensions?<br />
&gt;&gt; x(zeros(1,0))=y(zeros(1,0,1,0))<br />
x =<br />
     1     2     3     4     5</p>
<p>2. The source of my question in general goes back to the dark ages when Matlab didn&#8217;t have logical indexing and we would write code like this because we weren&#8217;t quite sure what to make of the assignment statement with an empty index.</p>
<p>ii=find(x&gt;100);<br />
if ~isempty(ii)<br />
  y(ii) = rhs<br />
end</p>
<p>Even now, we sometimes write:<br />
ii = x&gt;100;<br />
if any(ii),<br />
   y(ii) = rhs;<br />
end</p>
<p>This code was the motivation for my reference to &#8220;empty logicals&#8221; when I really meant a logical vector that&#8217;s all false.  I guess in this case ii has to have dimensions compatible with y and rhs and then y(ii) = rhs is basically a no-op (unless evaluation of rhs has side effects)?</p>
<p>3.  Which brings me to the question about the size of the matrices involved.  Using the if(any) construction above potentially removes the need to compute the rhs, which could be expensive.  My question was really if Matlab sees the expression x(ii) = rhs and sees that ii is empty (or all false), will it short circuit the assignment and not even bother to evaluate the rhs.  From what you described above, and what I see from some quick experiments, is that the short circuit does not occur because Matlab still checks that the assignment follows the rules for dimensions, and I guess it doesn&#8217;t know about potential side effects of evaluating rhs.  I suppose this makes sense and it&#8217;s easy enough to use the if(any) if wanted.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Loren</title>
		<link>http://blogs.mathworks.com/loren/2009/11/04/calculus-with-empty-arrays/#comment-30752</link>
		<dc:creator>Loren</dc:creator>
		<pubDate>Mon, 09 Nov 2009 13:22:56 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.mathworks.com/loren/2009/11/04/calculus-with-empty-arrays/#comment-30752</guid>
		<description>Tim-

That sparse behavior is, as you guessed, a bug.  It&#039;s entered in our database and will get fixed (not sure which release).  Thanks for pointing it out.

--Loren</description>
		<content:encoded><![CDATA[<p>Tim-</p>
<p>That sparse behavior is, as you guessed, a bug.  It&#8217;s entered in our database and will get fixed (not sure which release).  Thanks for pointing it out.</p>
<p>&#8211;Loren</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Loren</title>
		<link>http://blogs.mathworks.com/loren/2009/11/04/calculus-with-empty-arrays/#comment-30751</link>
		<dc:creator>Loren</dc:creator>
		<pubDate>Mon, 09 Nov 2009 13:21:22 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.mathworks.com/loren/2009/11/04/calculus-with-empty-arrays/#comment-30751</guid>
		<description>Armin-

Please read this article in the FAQ about floating point calculations and their accuracy:

http://matlabwiki.mathworks.com/MATLAB_FAQ#Why_is_0.3-0.2-0.1_not_equal_to_zero_.28or_similar.29.3F

--Loren</description>
		<content:encoded><![CDATA[<p>Armin-</p>
<p>Please read this article in the FAQ about floating point calculations and their accuracy:</p>
<p><a href="http://matlabwiki.mathworks.com/MATLAB_FAQ#Why_is_0.3-0.2-0.1_not_equal_to_zero_.28or_similar.29.3F" rel="nofollow">http://matlabwiki.mathworks.com/MATLAB_FAQ#Why_is_0.3-0.2-0.1_not_equal_to_zero_.28or_similar.29.3F</a></p>
<p>&#8211;Loren</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Armin</title>
		<link>http://blogs.mathworks.com/loren/2009/11/04/calculus-with-empty-arrays/#comment-30749</link>
		<dc:creator>Armin</dc:creator>
		<pubDate>Mon, 09 Nov 2009 10:34:02 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.mathworks.com/loren/2009/11/04/calculus-with-empty-arrays/#comment-30749</guid>
		<description>Dear Sir/Madam,
I have a problem with find function in the following code:

The result show &quot;Empty matrix: 1-by-0&quot; message for some value of Tuhs such as Tuhs(1)=0.1 which is seems illogical. I would be pleased to have your comments.</description>
		<content:encoded><![CDATA[<p>Dear Sir/Madam,<br />
I have a problem with find function in the following code:</p>
<p>The result show &#8220;Empty matrix: 1-by-0&#8243; message for some value of Tuhs such as Tuhs(1)=0.1 which is seems illogical. I would be pleased to have your comments.</p>
]]></content:encoded>
	</item>
</channel>
</rss>

