Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>There are three reasons I use <code>bsxfun</code> (<a href="http://www.mathworks.com/help/matlab/ref/bsxfun.html" rel="noreferrer">documentation</a>, <a href="http://blogs.mathworks.com/loren/2008/08/04/comparing-repmat-and-bsxfun-performance/" rel="noreferrer">blog link</a>)</p> <ol> <li><code>bsxfun</code> is faster than <code>repmat</code> (see below)</li> <li><code>bsxfun</code> requires less typing</li> <li>Using <code>bsxfun</code>, like using <code>accumarray</code>, makes me feel good about my understanding of Matlab.</li> </ol> <p><code>bsxfun</code> will replicate the input arrays along their "singleton dimensions", i.e. the dimensions along which the size of the array is 1, so that they match the size of the corresponding dimension of the other array. This is what is called "singleton expasion". As an aside, the singleton dimensions are the ones that will be dropped if you call <code>squeeze</code>.</p> <p>It is possible that for very small problems, the <code>repmat</code> approach is faster - but at that array size, both operations are so fast that it likely won't make any difference in terms of overall performance. There are two important reasons <code>bsxfun</code> is faster: (1) the calculation happens in compiled code, which means that the actual replication of the array never happens, and (2) <code>bsxfun</code> is one of the multithreaded Matlab functions.</p> <p>I have run a speed comparison between <code>repmat</code> and <code>bsxfun</code> with R2012b on my decently fast laptop.</p> <p><img src="https://i.stack.imgur.com/i2meT.png" alt="enter image description here"></p> <p>For me, <code>bsxfun</code> is about 3 times faster than <code>repmat</code>. The difference becomes more pronounced if the arrays get larger</p> <p><img src="https://i.stack.imgur.com/CZRMO.png" alt="enter image description here"></p> <p>The jump in runtime of <code>repmat</code> happens around an array size of 1Mb, which could have something to do with the size of my processor cache - <code>bsxfun</code> doesn't get as bad of a jump, because it only needs to allocate the output array.</p> <p>Below you find the code I used for timing:</p> <pre><code>n = 300; k=1; %# k=100 for the second graph a = ones(10,1); rr = zeros(n,1); bb=zeros(n,1); ntt=100; tt=zeros(ntt,1); for i=1:n; r = rand(1,i*k); for it=1:ntt; tic, x=bsxfun(@plus,a,r); tt(it)=toc; end; bb(i)=median(tt); for it=1:ntt; tic, y=repmat(a,1,i*k)+repmat(r,10,1); tt(it)=toc; end; rr(i)=median(tt); end </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload