No BSD License  

3.0

3.0 | 2 ratings Rate this file 31 Downloads (last 30 days) File Size: 6.54 KB File ID: #18685
image thumbnail

bsxfun

by

 

10 Feb 2008 (Updated )

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

| 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 file inspired 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.
Tags for This File   Please login to tag files.
Please login to add a comment or rating.
Comments and Ratings (9)
10 Apr 2013 li fangyuan  
10 Apr 2013 li fangyuan

good

23 Aug 2010 James Tursa

@ Harry Bedi: First change your default directory to the one where the bsxarg.c file is. Then issue the mex bsxarg.c command. (I haven't updated this submission to have a self-building mechanism yet.)

22 Aug 2010 Harry Bedi

Hi I am running v7.1 but I dont understand what I need to do get this to work with the aforementioned function. I keep getting the following error:
C:\PROGRAM FILES (X86)\MATLAB71\BIN\MEX.PL: Error: 'bsxarg.c' not found.
Could someone please let me know what file I need to update and where to get this to work?
Thanks

12 Jun 2009 James Tursa

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

15 Oct 2008 Wilman ZOU

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

Thank you ~~

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.

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!

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>.

Contact us