Be the first to rate this file! 237 downloads (last 30 days) File Size: 6.54 KB File ID: #18685

bsxfun

by James Tursa

 

10 Feb 2008 (Updated 11 Feb 2008)

No BSD License  

bsxfun is a functional substitute for MATLAB intrinsic of the same name

Download Now | Watch this File

File Information
Description

A poor-man's replacement for bsxfun for those users with earlier versions of MATLAB that do not have this intrinsic function available to them. Uses the mex routine bsxarg to physically generate the singleton expanded arrays, and then calls the function. This bsxfun m-file function is not as fast as the MATLAB intrinsic bsxfun (in some cases several times slower), but at least it gives you the same functionality.

bsxfun will apply an element-by-element binary operation to two arrays with singleton expansion enabled.

bsxfun is an m-file function that mimics the MATLAB intrinsic bsxfun. The m-file bsxfun differs from the MATLAB intrinsic bsxfun in the following ways:
  - Limited to 12 dimensions
  - Allows the 1st input to be a char string as well as a function handle
  - Physically does the singleton expansion of each input array
  - Requires the use of the bsxarg c-mex routine (included with this post for convenience)

This bsxfun m-file is mainly intended for users with older versions of MATLAB that do not have the MATLAB intrinsic bsxfun available.

 REQUIREMENT: You must build bsxarg first (see bsxarg.c file)

 The following are the relavent excerpts from the MATLAB doc:

 Syntax

 C = bsxfun(fun,A,B)

 Description

C = bsxfun(fun,A,B) applies an element-by-element binary operation to arrays A and B, with singleton expansion enabled. fun is a function handle or a character string specifying the name of a binary function, and can either be an M-file function or one of the following built-in functions:

   @plus Plus
   @minus Minus
   @times Array multiply
   @rdivide Right array divide
   @ldivide Left array divide
   @power Array power
   @max Binary maximum
   @min Binary minimum
   @rem Remainder after division
   @mod Modulus after division
   @atan2 Four quadrant inverse tangent
   @hypot Square root of sum of squares
   @eq Equal
   @ne Not equal
   @lt Less than
   @le Less than or equal to
   @gt Greater than
   @ge Greater than or equal to
   @and Element-wise logical AND
   @or Element-wise logical OR
   @xor Logical exclusive OR

If an M-file function is specified, it must be able to accept two input arrays of the same size as the result C.

Each dimension of A and B must either be equal to each other, or equal to 1. Whenever a dimension of A or B is singleton (equal to 1), the array is replicated along the dimension to match the other array. The array may be diminished if the corresponding dimension of the other array is 0.

The size of the output array C is equal to:

 max(size(A),size(B)).*(size(A)>0 & size(B)>0).

The bsxfun m-file will, of course, clash with the actual MATLAB intrinsic of the same name, but this is intentional and by design.

Acknowledgements
This submission has inspired the following:
Finding the Similar Entries: A Quantitative Approach based on CPU Runtime Behavior
MATLAB release MATLAB 7.2 (R2006a)
Other requirements Any C compiler, such as the built in lcc compiler, is needed to compile the associated bsxarg.c routine.
Zip File Content  
Other Files bsxarg.c,
bsxarg.m,
bsxfun.m
Tags for This File  
Everyone's Tags
Tags I've Applied
Add New Tags Please login to tag files.
Comments and Ratings (5)
11 Feb 2008 Doug Schwarz

Check out file 10333. I wrote it before bsxfun came out, but it's a drop-in replacement and does not replicate any data to perform its magic. It also will usually be slower than bsxfun.

You might also be interested in <http://www.frontiernet.net/~dmschwarz/genops.html&gt;.

11 Feb 2008 James Tursa

Thank you very much for the file links. It's kind of funny how this happened. A earlier NG post asked for a bsxfun replacement and there were no replies in this thread giving solutions in the FEX. So I searched the FEX for bsxfun and only found one function, reindex, that didn't do the complete job. So I figured there must be a need for this and wrote this replacement and posted it. But now that I have posted it, several people have come forward saying it is not needed because of the earlier solutions already in the FEX. That is fine, and I will happily withdraw my files if they are not needed. But if I had a hard time finding these other solutions, so will other people. Right now, the only files that result from a "bsxfun" search in the FEX are my files and the reindex file. Maybe you could add the "bsxfun" search word in your file posts so people like me can find them. If people can't find them they can't use them. I will download the ones I have been made aware of to compare with mine. Thanks again!

24 Sep 2008 Ivan Vavey

You can use repmat for physically expand dimensions instead writing a mex file. The key point to use bsxfun is memory concerns.

15 Oct 2008 Wilman ZOU

can i use this function in Matlab 7.0 ?
I only have this version .

Thank you ~~

12 Jun 2009 James Tursa

A better bsxfun substitute can be found here by Doug Schwarz:
http://www.mathworks.com/matlabcentral/fileexchange/23005

Please login to add a comment or rating.
Tag Activity for this File
Tag Applied By Date/Time
matrices James Tursa 22 Oct 2008 09:47:13
binary Cristina McIntire 06 Feb 2009 10:53:51
bsxarg Cristina McIntire 06 Feb 2009 10:53:51
bsxfun Cristina McIntire 06 Feb 2009 10:53:51
expansion Cristina McIntire 06 Feb 2009 10:53:51
operation Cristina McIntire 06 Feb 2009 10:53:51
 

MATLAB Central Terms of Use

NOTICE: Any content you submit to MATLAB Central, including personal information, is not subject to the protections which may be afforded information collected under other sections of The MathWorks, Inc. Web site. You are entirely responsible for all content that you upload, post, e-mail, transmit or otherwise make available via MATLAB Central. The MathWorks does not control the content posted by visitors to MATLAB Central and, does not guarantee the accuracy, integrity, or quality of such content. Under no circumstances will The MathWorks be liable in any way for any content not authored by The MathWorks, or any loss or damage of any kind incurred as a result of the use of any content posted, e-mailed, transmitted or otherwise made available via MATLAB Central. Read the complete Terms prior to use.

Contact us at files@mathworks.com