File Exchange

image thumbnail


version (6.54 KB) by James Tursa
bsxfun is a functional substitute for MATLAB intrinsic of the same name

1 Download

Updated 11 Feb 2008

No License

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:


C = bsxfun(fun,A,B)


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.

Cite As

James Tursa (2019). bsxfun (, MATLAB Central File Exchange. Retrieved .

Comments and Ratings (10)

What is array power.

li fangyuan

li fangyuan


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

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?

James Tursa

A better bsxfun substitute can be found here by Doug Schwarz:

Wilman ZOU

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

Thank you ~~

Ivan Vavey

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

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!

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

MATLAB Release Compatibility
Created with R2006a
Compatible with any release
Platform Compatibility
Windows macOS Linux