There are mainly two things I would like to research on about here -
There are six built-in relational operations for use with bsxfun
: @eq (equal)
, @ne (not-equal)
, @lt (less-than)
, @le (less-than or equal)
, @gt (greater-than)
and @ge (greater-than or equal)
. Lots of times we use them on floating point numbers and being relational operations, they output logical arrays. So, it got me curious, if the inherent expansion with bsxfun
when using these relational operations on floating point numbers involve actual replication of input elements and that is precisely my first question.
I would also like to know how this memory efficiency issue translates to the anonymous functions
when used with bsxfun
, again with the case of relational operations.
This is inspired by the runtime/speedup
tests performed for Comparing BSXFUN and REPMAT
.
Expansion with Custom Function The bsxfun function expands the vectors into matrices of the same size, which is an efficient way to evaluate fun for many combinations of the inputs.
In this case bsxfun is almost twice faster! It is useful and fast because it avoids explicit allocation of memory for matrices idx0 and idx1 , saving them to the memory, and then reading them again just to add them.
: bsxfun ( f , A , B ) The binary singleton expansion function performs broadcasting, that is, it applies a binary function f element-by-element to two array arguments A and B , and expands as necessary singleton dimensions in either input argument.
To perform memory tests to inquire about the points raised in the question, let's define the inputs A
and B
:
A = rand(M,N)
B = rand(1,N)
Here, M
and N
are the size parameters and are kept as really large numbers.
I would be using repmat
for comparisons as that seems like the closest alternative to bsxfun
. So, the idea here to run the bsxfun
and repmat
equivalent codes and watch out for the bumps in memory usages from the Task Manager (on Windows).
This solution that compared bsxfun
and repmat
for runtime efficiency led to the conclusions that using relational operations with bsxfun
is hugely runtime efficient, so it would be interesting to extend the basis of memory efficiency
to the comparisons.
Thus, the bsxfun
and repmat
equivalents would look something like these -
REPMAT version: A == repmat(B,size(A,1),1)
BSXFUN version: bsxfun(@eq,A,B))
On running the repmat
and then bsxfun
codes, the Windows Task Manager showed something like this with the first bump denoting the run for repmat
and the next one is for the bsxfun
one -
The repmat
bump has the same height as that when an actual copy of A
is created. This basically shows that repmat
makes an actual replication of B
and then does the equality check. Since, B
is to be replicated to a bigger floating point array, the memory requirements are huge as again shown in the memory graph earlier. On the other hand with bsxfun
, from its bump height it seems is not replicating the actual floating point values and that leads to an efficient memory usage.
Now, after converting both A
and B
to logical arrays, the memory usage bumps changed to this -
Thus, it suggests that repmat
was then able to optimize memory, as this time the replication was of logical datatype.
Using anonymous functions with bsxfun
: One can experiment a bit with the anonymous functions usage with bsxfun
and see if MATLAB shows the same smartness with it in optimizing memory requirements as with the built-in.
So, bsxfun(@eq,A,B)
could be replaced by bsxfun(@(k1,k2) k1==k2,A,B)
. The resultant memory usage with this built-in and anonymous function implementation when operated on floating point input arrays, resulted in a memory graph as shown below -
The plot indicates that the use of anonymous function keeps the memory efficiency as with the built-in, even though the runtime is hampered quite a bit. The test results were similar when other relational operations were used instead.
When working with relational operations on floating-point arrays, it's definitely preferable to use bsxfun
over repmat
for both runtime and memory efficiency. So, this just proves that there are more reasons to go with bsxfun
!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With