<?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 on: Comparing repmat and bsxfun Performance</title>
	<link>http://blogs.mathworks.com/loren/2008/08/04/comparing-repmat-and-bsxfun-performance/</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>Sun, 22 Nov 2009 23:20:54 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.3.1</generator>
		<item>
		<title>By: Tom Clark</title>
		<link>http://blogs.mathworks.com/loren/2008/08/04/comparing-repmat-and-bsxfun-performance/#comment-30543</link>
		<dc:creator>Tom Clark</dc:creator>
		<pubDate>Wed, 19 Aug 2009 13:26:17 +0000</pubDate>
		<guid>http://blogs.mathworks.com/loren/2008/08/04/comparing-repmat-and-bsxfun-performance/#comment-30543</guid>
		<description>Loren,

You're probably right. I'll put it on the backburner!

If anyone would like this idea implemented, drop me a message (t.clarkremovespam@cantab.net) and I'll give it a shot.

Tom</description>
		<content:encoded><![CDATA[<p>Loren,</p>
<p>You&#8217;re probably right. I&#8217;ll put it on the backburner!</p>
<p>If anyone would like this idea implemented, drop me a message (t.clarkremovespam@cantab.net) and I&#8217;ll give it a shot.</p>
<p>Tom</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Loren</title>
		<link>http://blogs.mathworks.com/loren/2008/08/04/comparing-repmat-and-bsxfun-performance/#comment-30531</link>
		<dc:creator>Loren</dc:creator>
		<pubDate>Fri, 14 Aug 2009 11:38:54 +0000</pubDate>
		<guid>http://blogs.mathworks.com/loren/2008/08/04/comparing-repmat-and-bsxfun-performance/#comment-30531</guid>
		<description>Tom-

Perhaps easier to hide the bsxfun calls, but the need to convert and then operate doesn't save the user steps.  Interesting idea with interesting trade-offs.

My guess is that unless the arrays are large, you will pay a noticeable price (relatively) for the object.

--Loren</description>
		<content:encoded><![CDATA[<p>Tom-</p>
<p>Perhaps easier to hide the bsxfun calls, but the need to convert and then operate doesn&#8217;t save the user steps.  Interesting idea with interesting trade-offs.</p>
<p>My guess is that unless the arrays are large, you will pay a noticeable price (relatively) for the object.</p>
<p>&#8211;Loren</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Tom Clark</title>
		<link>http://blogs.mathworks.com/loren/2008/08/04/comparing-repmat-and-bsxfun-performance/#comment-30529</link>
		<dc:creator>Tom Clark</dc:creator>
		<pubDate>Fri, 14 Aug 2009 10:56:52 +0000</pubDate>
		<guid>http://blogs.mathworks.com/loren/2008/08/04/comparing-repmat-and-bsxfun-performance/#comment-30529</guid>
		<description>Hi Loren,

Sorry to resurrect an old blog - I just had a thought of a way to enhance readability for bsxfun.

What about creating a bsxfun type class? You could then overload all the operators (+ - etc) to use the 'ugly' call to bsxfun simply.

Let's look at a simple example for which you could use bsxfun:
&lt;pre&gt;

% Generates an error unless you use bsxfun, or repmat etc...
A = rand(10);
B = ones(10,1);
A = A - B;

&lt;/pre&gt;


But if we designated a class:
&lt;pre&gt;

A = rand(10);
B = ones(10,1);
% Make a variable of type bsxclass. 
% Values of B are kept in the private
% data of the class.
B_exp = bsxclass(B)
% B_exp is effectively an array which
% can be expanded along a singleton
% dimension
A = A - B_exp;

&lt;/pre&gt;


... so the class definition contains the horrible calls to bsxfun, and your code contains simple operator statements.

What does everyone think? Would such a class get used? Will there be a significant functional overhead caused by overloading the method / class invokation?

.. i.e. should I sit down and write it??!

Tom</description>
		<content:encoded><![CDATA[<p>Hi Loren,</p>
<p>Sorry to resurrect an old blog - I just had a thought of a way to enhance readability for bsxfun.</p>
<p>What about creating a bsxfun type class? You could then overload all the operators (+ - etc) to use the &#8216;ugly&#8217; call to bsxfun simply.</p>
<p>Let&#8217;s look at a simple example for which you could use bsxfun:</p>
<pre>

% Generates an error unless you use bsxfun, or repmat etc...
A = rand(10);
B = ones(10,1);
A = A - B;
</pre>
<p>But if we designated a class:</p>
<pre>

A = rand(10);
B = ones(10,1);
% Make a variable of type bsxclass.
% Values of B are kept in the private
% data of the class.
B_exp = bsxclass(B)
% B_exp is effectively an array which
% can be expanded along a singleton
% dimension
A = A - B_exp;
</pre>
<p>&#8230; so the class definition contains the horrible calls to bsxfun, and your code contains simple operator statements.</p>
<p>What does everyone think? Would such a class get used? Will there be a significant functional overhead caused by overloading the method / class invokation?</p>
<p>.. i.e. should I sit down and write it??!</p>
<p>Tom</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Loren</title>
		<link>http://blogs.mathworks.com/loren/2008/08/04/comparing-repmat-and-bsxfun-performance/#comment-29982</link>
		<dc:creator>Loren</dc:creator>
		<pubDate>Fri, 23 Jan 2009 19:19:29 +0000</pubDate>
		<guid>http://blogs.mathworks.com/loren/2008/08/04/comparing-repmat-and-bsxfun-performance/#comment-29982</guid>
		<description>Stephen-

We'll put this idea on our list to consider for future enhancements.

--Loren</description>
		<content:encoded><![CDATA[<p>Stephen-</p>
<p>We&#8217;ll put this idea on our list to consider for future enhancements.</p>
<p>&#8211;Loren</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: StephenLL</title>
		<link>http://blogs.mathworks.com/loren/2008/08/04/comparing-repmat-and-bsxfun-performance/#comment-29977</link>
		<dc:creator>StephenLL</dc:creator>
		<pubDate>Thu, 22 Jan 2009 18:56:35 +0000</pubDate>
		<guid>http://blogs.mathworks.com/loren/2008/08/04/comparing-repmat-and-bsxfun-performance/#comment-29977</guid>
		<description>This may be asking too much but is it possible to go beyond binary expansion?

I would think that an arbitrary amount of expansion would be useful.

Something along the lines:

x = rand(10,1)
y = rand(1,10)
z = rand([1,1,5])

g = bsxfun(@betainc,x,y,z);

Thanks for listening.

Stephen</description>
		<content:encoded><![CDATA[<p>This may be asking too much but is it possible to go beyond binary expansion?</p>
<p>I would think that an arbitrary amount of expansion would be useful.</p>
<p>Something along the lines:</p>
<p>x = rand(10,1)<br />
y = rand(1,10)<br />
z = rand([1,1,5])</p>
<p>g = bsxfun(@betainc,x,y,z);</p>
<p>Thanks for listening.</p>
<p>Stephen</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Jinhui Bai</title>
		<link>http://blogs.mathworks.com/loren/2008/08/04/comparing-repmat-and-bsxfun-performance/#comment-29975</link>
		<dc:creator>Jinhui Bai</dc:creator>
		<pubDate>Thu, 22 Jan 2009 15:43:24 +0000</pubDate>
		<guid>http://blogs.mathworks.com/loren/2008/08/04/comparing-repmat-and-bsxfun-performance/#comment-29975</guid>
		<description>Loren,
Thanks for the answer on the design philosophy. At least now I know that it is not because of technical and efficiency reasons in implementation.

My interpretation literally comes from the behavior of repmat, which means [1 2 1 2] intead of [1 1 2 2]. But I agree that scalar expansion is an intuitive concept in linear algebra, but repmat is a MATLAB-specific concept. From the ease-of-use point of view, repmat-based interpretation may not be as clear as singleton expansion, especially for new users.

Jinhui</description>
		<content:encoded><![CDATA[<p>Loren,<br />
Thanks for the answer on the design philosophy. At least now I know that it is not because of technical and efficiency reasons in implementation.</p>
<p>My interpretation literally comes from the behavior of repmat, which means [1 2 1 2] intead of [1 1 2 2]. But I agree that scalar expansion is an intuitive concept in linear algebra, but repmat is a MATLAB-specific concept. From the ease-of-use point of view, repmat-based interpretation may not be as clear as singleton expansion, especially for new users.</p>
<p>Jinhui</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Loren</title>
		<link>http://blogs.mathworks.com/loren/2008/08/04/comparing-repmat-and-bsxfun-performance/#comment-29974</link>
		<dc:creator>Loren</dc:creator>
		<pubDate>Thu, 22 Jan 2009 15:32:13 +0000</pubDate>
		<guid>http://blogs.mathworks.com/loren/2008/08/04/comparing-repmat-and-bsxfun-performance/#comment-29974</guid>
		<description>Jinhui-

MATLAB has a traditional for scalar expansion, hence the behavior of bsxfun.  If we did allow matches like you suggest, it could mean different things.  Replicate the 2 rows in order multiple times, i.e., 1 2 1 2 1 2, OR it could be replicate each "the right number of times", i.e., 1 1 2 2.  It didn't seem worth the confusion since we didn't know which scenario was more likely of real interest.

--Loren</description>
		<content:encoded><![CDATA[<p>Jinhui-</p>
<p>MATLAB has a traditional for scalar expansion, hence the behavior of bsxfun.  If we did allow matches like you suggest, it could mean different things.  Replicate the 2 rows in order multiple times, i.e., 1 2 1 2 1 2, OR it could be replicate each &#8220;the right number of times&#8221;, i.e., 1 1 2 2.  It didn&#8217;t seem worth the confusion since we didn&#8217;t know which scenario was more likely of real interest.</p>
<p>&#8211;Loren</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Jinhui</title>
		<link>http://blogs.mathworks.com/loren/2008/08/04/comparing-repmat-and-bsxfun-performance/#comment-29973</link>
		<dc:creator>Jinhui</dc:creator>
		<pubDate>Thu, 22 Jan 2009 15:26:00 +0000</pubDate>
		<guid>http://blogs.mathworks.com/loren/2008/08/04/comparing-repmat-and-bsxfun-performance/#comment-29973</guid>
		<description>Loren,
I use bsxfun quite frequently and like it a lot. Indeed I found that many MATLAB toolbox functions are rewritten with bsxfun recently (say, repmat now uses bsxfun in sparse matrix case, spline interpolation also uses bsxfun, etc).

I have one question on the design of the function,though. Why is the function restricted to singleton expansion instead of more general integer-multiple expansion. What I mean can be illustrated more easily through an example.

% an example not work in current implementation
a = rand(4,6);
b = rand(2,6);
c = bsxfun(@plus,a,b); % does NOT work since it violates SINGLETON requirement
Conceptually bsxfun(@plus,a,b) can have a reasonable interpretation as "a + repmat(b,[2 1]);", here 2 comes from size(a,1)/size(b,1). The well-defined nature here comes from mod(size(a,1),size(b,1)) == 0 so that mismatched dimension is an integer multiple of the other. Obviously singleton expansion is a special case of this.

If an extension of bsxfun implements this, it will eliminate the need for repmat for more cases, which seems to be in the spirit of design of bsxfun (although we need to find a different interpretation for letter "s" in bsxfun :-)).

From my experience of C MEX programming, the implementation of integer-multiple expansion should be similar to singleton expansion which, I guess, involves a different stride for pointers. Or am I missing something?

Jinhui</description>
		<content:encoded><![CDATA[<p>Loren,<br />
I use bsxfun quite frequently and like it a lot. Indeed I found that many MATLAB toolbox functions are rewritten with bsxfun recently (say, repmat now uses bsxfun in sparse matrix case, spline interpolation also uses bsxfun, etc).</p>
<p>I have one question on the design of the function,though. Why is the function restricted to singleton expansion instead of more general integer-multiple expansion. What I mean can be illustrated more easily through an example.</p>
<p>% an example not work in current implementation<br />
a = rand(4,6);<br />
b = rand(2,6);<br />
c = bsxfun(@plus,a,b); % does NOT work since it violates SINGLETON requirement<br />
Conceptually bsxfun(@plus,a,b) can have a reasonable interpretation as &#8220;a + repmat(b,[2 1]);&#8221;, here 2 comes from size(a,1)/size(b,1). The well-defined nature here comes from mod(size(a,1),size(b,1)) == 0 so that mismatched dimension is an integer multiple of the other. Obviously singleton expansion is a special case of this.</p>
<p>If an extension of bsxfun implements this, it will eliminate the need for repmat for more cases, which seems to be in the spirit of design of bsxfun (although we need to find a different interpretation for letter &#8220;s&#8221; in bsxfun :-)).</p>
<p>From my experience of C MEX programming, the implementation of integer-multiple expansion should be similar to singleton expansion which, I guess, involves a different stride for pointers. Or am I missing something?</p>
<p>Jinhui</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Daniel</title>
		<link>http://blogs.mathworks.com/loren/2008/08/04/comparing-repmat-and-bsxfun-performance/#comment-29972</link>
		<dc:creator>Daniel</dc:creator>
		<pubDate>Thu, 22 Jan 2009 13:24:30 +0000</pubDate>
		<guid>http://blogs.mathworks.com/loren/2008/08/04/comparing-repmat-and-bsxfun-performance/#comment-29972</guid>
		<description>When this post came around, I read it, and didn't understand bsxfun. Now I understand bsxfun and I realize that bsxfun is the solution to alot of grief I have had over the years. Countless times I have sworn over the fact that I cannot add an array to a matrix, and now I realize that this bsxfun everyone was talking about was the solution.

Nifty.

--DA</description>
		<content:encoded><![CDATA[<p>When this post came around, I read it, and didn&#8217;t understand bsxfun. Now I understand bsxfun and I realize that bsxfun is the solution to alot of grief I have had over the years. Countless times I have sworn over the fact that I cannot add an array to a matrix, and now I realize that this bsxfun everyone was talking about was the solution.</p>
<p>Nifty.</p>
<p>&#8211;DA</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Gautam Vallabha</title>
		<link>http://blogs.mathworks.com/loren/2008/08/04/comparing-repmat-and-bsxfun-performance/#comment-29666</link>
		<dc:creator>Gautam Vallabha</dc:creator>
		<pubDate>Thu, 07 Aug 2008 16:15:20 +0000</pubDate>
		<guid>http://blogs.mathworks.com/loren/2008/08/04/comparing-repmat-and-bsxfun-performance/#comment-29666</guid>
		<description>A B asks a good question about readability of BSXFUN code. One difficulty is the name BSXFUN. It is an accurate description (Binary Singleton eXpansion FUNction), but on first glance the acronym is unintuitive and forbidding. I wonder if having an alias for it would help:

----------
A = rand(3,3);
match_dimensions = @(varargin) bsxfun(varargin{:});
B = match_dimensions(@minus, A, mean(A))
----------

Or, because we are dealing with binary operators, we can try to mimic the infix notation:

----------
match_dimensions = @(a,op,b) bsxfun(op,a,b);
B = match_dimensions(A, @minus, mean(A))
----------

Gautam</description>
		<content:encoded><![CDATA[<p>A B asks a good question about readability of BSXFUN code. One difficulty is the name BSXFUN. It is an accurate description (Binary Singleton eXpansion FUNction), but on first glance the acronym is unintuitive and forbidding. I wonder if having an alias for it would help:</p>
<p>&#8212;&#8212;&#8212;-<br />
A = rand(3,3);<br />
match_dimensions = @(varargin) bsxfun(varargin{:});<br />
B = match_dimensions(@minus, A, mean(A))<br />
&#8212;&#8212;&#8212;-</p>
<p>Or, because we are dealing with binary operators, we can try to mimic the infix notation:</p>
<p>&#8212;&#8212;&#8212;-<br />
match_dimensions = @(a,op,b) bsxfun(op,a,b);<br />
B = match_dimensions(A, @minus, mean(A))<br />
&#8212;&#8212;&#8212;-</p>
<p>Gautam</p>
]]></content:encoded>
	</item>
</channel>
</rss>
