<?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: Inverting Logic in an Algorithm</title>
	<atom:link href="http://blogs.mathworks.com/loren/2006/01/18/think-about-your-algorithm/feed/" rel="self" type="application/rss+xml" />
	<link>http://blogs.mathworks.com/loren/2006/01/18/think-about-your-algorithm/</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: Mykola</title>
		<link>http://blogs.mathworks.com/loren/2006/01/18/think-about-your-algorithm/#comment-31001</link>
		<dc:creator>Mykola</dc:creator>
		<pubDate>Fri, 22 Jan 2010 13:35:16 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.mathworks.com/loren-prototype/?p=13#comment-31001</guid>
		<description>Loren, your code and your recommendations helped me a lot!

Thank you very much!

-Mykola</description>
		<content:encoded><![CDATA[<p>Loren, your code and your recommendations helped me a lot!</p>
<p>Thank you very much!</p>
<p>-Mykola</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Loren</title>
		<link>http://blogs.mathworks.com/loren/2006/01/18/think-about-your-algorithm/#comment-30999</link>
		<dc:creator>Loren</dc:creator>
		<pubDate>Fri, 22 Jan 2010 11:46:26 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.mathworks.com/loren-prototype/?p=13#comment-30999</guid>
		<description>Mykola,

Logical indexing essentially deletes the zero elements and since there are not necessarily the same number left in each column, MATLAB returns the results in a single column vector.  You should read more in the documentation about logical indexing.  To get the result you want, you could instead try this:

&lt;pre class=&quot;code&quot;&gt;
R = magic(3);
k = logical([0 1 1 ; 1 1 1; 0 0 0]);
sum(R.*k)
&lt;/pre&gt;

--Loren</description>
		<content:encoded><![CDATA[<p>Mykola,</p>
<p>Logical indexing essentially deletes the zero elements and since there are not necessarily the same number left in each column, MATLAB returns the results in a single column vector.  You should read more in the documentation about logical indexing.  To get the result you want, you could instead try this:</p>
<pre class="code">
R = magic(3);
k = logical([0 1 1 ; 1 1 1; 0 0 0]);
sum(R.*k)
</pre>
<p>&#8211;Loren</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Mykola</title>
		<link>http://blogs.mathworks.com/loren/2006/01/18/think-about-your-algorithm/#comment-30998</link>
		<dc:creator>Mykola</dc:creator>
		<pubDate>Fri, 22 Jan 2010 09:36:32 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.mathworks.com/loren-prototype/?p=13#comment-30998</guid>
		<description>Hi Loren!
I had a piece of code
&lt;pre&gt;
m = 2139;
n = 10000;
% a vector of sums
S_stamp = single(rand(m, 1)*10);
tic
for i = 1:10
    k = rand(m, n)&gt;0.9;
    R = S_stamp(:, ones(1, n, &#039;int8&#039;));
    A = zeros(m, n, &#039;single&#039;);
    % I take sums, that meet the k condition
    A(k) = R(k);
    clear R k
    % than I sum them
    S.([&#039;SS&#039; int2str(i)]) = sum(A);
    clear A
end
toc
% std([S.SS1 S.SS2 ..]
&lt;/pre&gt;



After I read your article I did so:
I inverted k condition

&lt;pre&gt;
z = single(0);
tic
for i = 1:10
    k = rand(m, n)&lt;0.9;
    R = S_stamp(:, ones(1, n, &#039;int8&#039;));
    %I send zeros to the sums that now don&#039;t meet the initial k condition
    R(k) = z;
    clear k
    % than I sum the sums ans don&#039;t need to generate  A = zeros ... THANK you
    % very much!
    S.([&#039;SS&#039; int2str(i)]) = sum(R);
    clear R
end
toc
&lt;/pre&gt;


I got

Elapsed time is 23.300569 seconds.
Elapsed time is 22.413216 seconds.
It is a VERY good time improvement for my program! Thank you, Loren!

Please, Loren, can you help me ?
Than I decided to do so

&lt;pre&gt;
for i = 1:10
    k = rand(m, n)&gt;0.9;
    R = S_stamp(:, ones(1, n, &#039;int8&#039;));
    S.([&#039;SS&#039; int2str(i)]) = sum(R(k));
    clear R
end
&lt;/pre&gt;


but ...
&lt;pre&gt;
R = magic(3);
k = logical([0 1 1 ; 1 1 1; 0 0 0]);
R(k)
&lt;/pre&gt;

ans =

     3
     1
     5
     6
     7
     
It seemed to me that R looses it&#039;t structure
if I 
&lt;pre&gt;
sum(R(k))
&lt;/pre&gt;
I don&#039;t get what I am looking for

Thank you very much for any tip!
Mykola</description>
		<content:encoded><![CDATA[<p>Hi Loren!<br />
I had a piece of code</p>
<pre>
m = 2139;
n = 10000;
% a vector of sums
S_stamp = single(rand(m, 1)*10);
tic
for i = 1:10
    k = rand(m, n)&gt;0.9;
    R = S_stamp(:, ones(1, n, 'int8'));
    A = zeros(m, n, 'single');
    % I take sums, that meet the k condition
    A(k) = R(k);
    clear R k
    % than I sum them
    S.(['SS' int2str(i)]) = sum(A);
    clear A
end
toc
% std([S.SS1 S.SS2 ..]
</pre>
<p>After I read your article I did so:<br />
I inverted k condition</p>
<pre>
z = single(0);
tic
for i = 1:10
    k = rand(m, n)&lt;0.9;
    R = S_stamp(:, ones(1, n, 'int8'));
    %I send zeros to the sums that now don't meet the initial k condition
    R(k) = z;
    clear k
    % than I sum the sums ans don't need to generate  A = zeros ... THANK you
    % very much!
    S.(['SS' int2str(i)]) = sum(R);
    clear R
end
toc
</pre>
<p>I got</p>
<p>Elapsed time is 23.300569 seconds.<br />
Elapsed time is 22.413216 seconds.<br />
It is a VERY good time improvement for my program! Thank you, Loren!</p>
<p>Please, Loren, can you help me ?<br />
Than I decided to do so</p>
<pre>
for i = 1:10
    k = rand(m, n)&gt;0.9;
    R = S_stamp(:, ones(1, n, 'int8'));
    S.(['SS' int2str(i)]) = sum(R(k));
    clear R
end
</pre>
<p>but &#8230;</p>
<pre>
R = magic(3);
k = logical([0 1 1 ; 1 1 1; 0 0 0]);
R(k)
</pre>
<p>ans =</p>
<p>     3<br />
     1<br />
     5<br />
     6<br />
     7</p>
<p>It seemed to me that R looses it&#8217;t structure<br />
if I </p>
<pre>
sum(R(k))
</pre>
<p>I don&#8217;t get what I am looking for</p>
<p>Thank you very much for any tip!<br />
Mykola</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Loren</title>
		<link>http://blogs.mathworks.com/loren/2006/01/18/think-about-your-algorithm/#comment-16396</link>
		<dc:creator>Loren</dc:creator>
		<pubDate>Tue, 21 Aug 2007 19:11:19 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.mathworks.com/loren-prototype/?p=13#comment-16396</guid>
		<description>Vijay-

There is not a way to change the default.  But you can do things like:

&lt;pre class=&quot;code&quot;&gt;
x(1,400) = single(0);
&lt;/pre&gt;

and write code that is agnostic with respect to what kind of floating point, e.g.,


&lt;pre class=&quot;code&quot;&gt;
y = zeros(1,n,class(input1));
&lt;/pre&gt;

--Loren</description>
		<content:encoded><![CDATA[<p>Vijay-</p>
<p>There is not a way to change the default.  But you can do things like:</p>
<pre class="code">
x(1,400) = single(0);
</pre>
<p>and write code that is agnostic with respect to what kind of floating point, e.g.,</p>
<pre class="code">
y = zeros(1,n,class(input1));
</pre>
<p>&#8211;Loren</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Vijay</title>
		<link>http://blogs.mathworks.com/loren/2006/01/18/think-about-your-algorithm/#comment-16395</link>
		<dc:creator>Vijay</dc:creator>
		<pubDate>Tue, 21 Aug 2007 18:35:37 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.mathworks.com/loren-prototype/?p=13#comment-16395</guid>
		<description>Hi Loren,
Is there anyway to globally cast the class of all variables to &#039;single&#039; instead of &#039;double&#039;? For instance if I type a=10, I would like &#039;a&#039; to be of class &#039;single&#039; instead of &#039;double&#039;. Of course, I could do a=single(10), but I can avoid the additional casting step if I can set &#039;single&#039; to be the default class.
Thanks,
Vijay</description>
		<content:encoded><![CDATA[<p>Hi Loren,<br />
Is there anyway to globally cast the class of all variables to &#8216;single&#8217; instead of &#8216;double&#8217;? For instance if I type a=10, I would like &#8216;a&#8217; to be of class &#8216;single&#8217; instead of &#8216;double&#8217;. Of course, I could do a=single(10), but I can avoid the additional casting step if I can set &#8216;single&#8217; to be the default class.<br />
Thanks,<br />
Vijay</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Thomas Kite</title>
		<link>http://blogs.mathworks.com/loren/2006/01/18/think-about-your-algorithm/#comment-27</link>
		<dc:creator>Thomas Kite</dc:creator>
		<pubDate>Mon, 06 Feb 2006 18:26:50 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.mathworks.com/loren-prototype/?p=13#comment-27</guid>
		<description>To get around the sinc(0) = 1 problem, I add eps to the arguments in the numerator and denominator: sin(x + eps) ./ (x + eps).  This gives negligible error and avoids the division by zero problem (unless x is -eps, which seems very unlikely).  The following code:

% Compare sinc on a random vector
x = randn(1000000, 1);
y1 = sin(pi * x) ./ (pi * x); y1(x==0) = 1;
y2 = sin(pi .* x + eps) ./ (pi .* x + eps);

% Compare sinc on a zero vector
x = zeros(size(x));
y1 = sin(pi * x) ./ (pi * x); y1(x==0) = 1;
y2 = sin(pi .* x + eps) ./ (pi .* x + eps);

gives results within eps in the first case and identical results in the second.  Using the profiler, we see execution times of 0.20/0.19 seconds for the random vector, and 0.42/0.05 seconds for the zero vector.  Thus in both instances the &#039;add eps&#039; version is faster.  The results would also seem to bear out Peter&#039;s comments above, that processing with NaNs is slow.</description>
		<content:encoded><![CDATA[<p>To get around the sinc(0) = 1 problem, I add eps to the arguments in the numerator and denominator: sin(x + eps) ./ (x + eps).  This gives negligible error and avoids the division by zero problem (unless x is -eps, which seems very unlikely).  The following code:</p>
<p>% Compare sinc on a random vector<br />
x = randn(1000000, 1);<br />
y1 = sin(pi * x) ./ (pi * x); y1(x==0) = 1;<br />
y2 = sin(pi .* x + eps) ./ (pi .* x + eps);</p>
<p>% Compare sinc on a zero vector<br />
x = zeros(size(x));<br />
y1 = sin(pi * x) ./ (pi * x); y1(x==0) = 1;<br />
y2 = sin(pi .* x + eps) ./ (pi .* x + eps);</p>
<p>gives results within eps in the first case and identical results in the second.  Using the profiler, we see execution times of 0.20/0.19 seconds for the random vector, and 0.42/0.05 seconds for the zero vector.  Thus in both instances the &#8216;add eps&#8217; version is faster.  The results would also seem to bear out Peter&#8217;s comments above, that processing with NaNs is slow.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Peter Boettcher</title>
		<link>http://blogs.mathworks.com/loren/2006/01/18/think-about-your-algorithm/#comment-23</link>
		<dc:creator>Peter Boettcher</dc:creator>
		<pubDate>Thu, 02 Feb 2006 14:36:21 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.mathworks.com/loren-prototype/?p=13#comment-23</guid>
		<description>Skipping the 0 check could result in much worse penalty than copying the data array.  On the Pentium IV architecture, FPU operations that result in NaN or Inf are very expensive.  Divide-by-zero warnings being on or off doesn&#039;t matter.  For example:


warning(&#039;off&#039;, &#039;MATLAB:divideByZero&#039;);

x = round(rand(1,1e7));  % Roughly equal number of zeros and ones
y = x + 1;               % No zeros

tic; s = 1./x; toc
tic; s = 1./y; toc



The timing results on my machine come out to 2.2s for the first case, with 1/2 zeros, while the second case only takes 0.34 seconds.

Of course, your point in this was that the number of input elements that are exactly zero is very small.  When I change the proportion to 10000 zeros (1%), then it&#039;s only 0.38 vs the 0.34.

Just something to consider for other optimizations when there are many zeros.  The same thing will apply to processing with NaNs.</description>
		<content:encoded><![CDATA[<p>Skipping the 0 check could result in much worse penalty than copying the data array.  On the Pentium IV architecture, FPU operations that result in NaN or Inf are very expensive.  Divide-by-zero warnings being on or off doesn&#8217;t matter.  For example:</p>
<p>warning(&#8216;off&#8217;, &#8216;MATLAB:divideByZero&#8217;);</p>
<p>x = round(rand(1,1e7));  % Roughly equal number of zeros and ones<br />
y = x + 1;               % No zeros</p>
<p>tic; s = 1./x; toc<br />
tic; s = 1./y; toc</p>
<p>The timing results on my machine come out to 2.2s for the first case, with 1/2 zeros, while the second case only takes 0.34 seconds.</p>
<p>Of course, your point in this was that the number of input elements that are exactly zero is very small.  When I change the proportion to 10000 zeros (1%), then it&#8217;s only 0.38 vs the 0.34.</p>
<p>Just something to consider for other optimizations when there are many zeros.  The same thing will apply to processing with NaNs.</p>
]]></content:encoded>
	</item>
</channel>
</rss>

