4

I did bump into this question while searching for this topic, but this one seems to be outdated.

Reading https://blogs.mathworks.com/loren/2016/10/24/matlab-arithmetic-expands-in-r2016b , implicit expansion was introduced in 2016b, but I can still find the reference codes in the papers using bsxfun for arithmetic expansion. So I assume that there are some circumstances that make bsxfun preferable to other methods.

I did compare the speeds between bsxfun, repmat, and implicit expansion (I used the code of Jonas from the link)

The below shows the comparison in calculation time using tic toc:

Comparison of calculation time using tic and toc

which shows that implicit expansion is clearly faster than bsxfun or repmat. Is there any reason to use bsxfun nowadays?

Here is the code I used to compare the speed:

n = 300;
k=100; %# 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);
   for it=1:ntt;
      tic,
      z = a + r;
      tt(it) = toc;
   end;
   gg(i) = median(tt);
end
figure;
plot(bb,'b')
hold on
plot(rr,'r')
plot(gg,'g')
legend(["bsxfun","repmat","implicit"])
1
  • 1
    Note also that implicit expansion seems to do some (unexpected) optimizations that bsxfun does not do
    – Luis Mendo
    Nov 25, 2021 at 11:47

1 Answer 1

7

All bsxfun does is Binary Singleton eXpansion. It's more typing than the, now usual, implicit expansion. I'd guess The MathWorks kept bsxfun around for backwards compatibility, but no longer works on it; it might even internally just map to implicit expansion.

The documentation on bsxfun states:

It is recommended that you replace most uses of bsxfun with direct calls to the functions and operators that support implicit expansion. Compared to using bsxfun, implicit expansion offers faster speed of execution, better memory usage, and improved readability of code. For more information, see Compatible Array Sizes for Basic Operations.

Additionally, implicit expansion seems to have internal optimisations beyond what bsxfun does, see this question of mine.
More helpful links can be found in this answer by nirvana-msu, amongst others to blogs by MathWorks employees discussing this.

So I'd say that the only reason to use bsxfun instead of implicit expansion would be if you'd run the code on a pre-2016b version of MATLAB.

3
  • 3
    What a great tradition reading the documentation is :D
    – Wolfie
    Nov 25, 2021 at 11:23
  • 1
    It might also be useful if the binary operation applied does not do implicit singleton expansion. Maybe it’s a MEX-file function you wrote yourself. But yes, backwards compatibility is a very important reason to keep it around. Nov 25, 2021 at 13:31
  • 1
    @CrisLuengo that's probably implied in the docs through most uses, rather than all uses.
    – Adriaan
    Nov 25, 2021 at 14:05

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.