No BSD License  

Highlights from
bsxarg

Be the first to rate this file! 1 Download (last 30 days) File Size: 4.9 KB File ID: #18686
image thumbnail

bsxarg

by James Tursa

 

10 Feb 2008 (Updated 11 Feb 2008)

bsxarg returns the physically expanded arrays associated with bsxfun

| Watch this File

File Information
Description

bsxarg returns the singleton expanded arrays that are virtually used in bsxfun. bsxarg is limited to a maximum of 12 dimensions. The MATLAB intrinsic function bsxfun performs binary operations on input arrays, where singleton dimensions are virtually expanded to perform the operation. bsxarg will actually do the singleton expansion and return the physically expanded arrays.
 
 Building (one-time only):
 
 >> mex -setup
   (then follow instructions to select a C / C++ compiler of your choice)
 >> mex bsxarg.c

 Syntax
 
 [C D] = bsxarg(A,B)

 Description
 
 C = the expanded version of A.
 D = the expanded version of B.
 
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 virtually 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 arrays C and D are equal to:
 
  max(size(A),size(B)).*(size(A)>0 & size(B)>0).

User's with older versions of MATLAB that do not have the MATLAB intrinsic bsxfun available to them can use this simple m-file to get that capability:
 
   function C = bsxfun(fun,A,B)
   if( nargin ~= 3 )
       error('Need 3 arguments for bsxfun')
   end
   [AX BX] = bsxarg(A,B);
   if( ischar(fun) )
       C = eval([fun '(AX,BX)']);
   else
       C = fun(AX,BX);
   end
   return
   end

MATLAB release MATLAB 7.2 (R2006a)
Other requirements Any C compiler, such as the built in lcc compiler, is needed to compile the bsxarg.c mex file.
Tags for This File  
Everyone's Tags
Tags I've Applied
Add New Tags Please login to tag files.
Comments and Ratings (2)
11 Feb 2008 Urs (us) Schwarz

users who do not have BSXFUN could also use one of the many(!) solutions found on CSSM (ML's NG), which typically uses a simple REPMAT engine...
us

12 Feb 2008 James Tursa

Thank you for your comments. Regarding BSXFUN, I did in fact look at the NG and found a recent thread where a user requested help on a replacement for BSXFUN. There were no responses mentioning any kind of generic solutions using REPMAT, only specific solutions for OP's particular problem. Then I searched the FEX for "bsxfun" and found only one function, REINDEX, available, and that one did not do the complete job. So I assumed that this function was not available in the FEX and wrote a solution. I have since been informed that there is another solution, genop, posted by Douglas Schwarz (file 10333), that essentially duplicates the BSXFUN function. Unfortunately, this does not show up in the search when you look for "bsxfun" in the FEX, so there may be many users (such as me) who searched but couldn't find it. You mention that you know of many solutions using REPMAT. Please give me the links to them so that I can examine them and compare them with my code. I would be more than glad to withdraw my BSXFUN submission if there are already better methods available. Thanks.

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:14
binary Cristina McIntire 06 Feb 2009 10:53:14
bsxfun Cristina McIntire 06 Feb 2009 10:53:14
bsxarg Cristina McIntire 06 Feb 2009 10:53:14
expansion Cristina McIntire 06 Feb 2009 10:53:14
operation Cristina McIntire 06 Feb 2009 10:53:14

Contact us at files@mathworks.com