Be the first to rate this file! 10 downloads (last 30 days) File Size: 4.31 KB File ID: #23084

Binary array expansion function

by Paolo de Leva

 

23 Feb 2009

Code covered by the BSD License  

element-by-element binary operations (e.g. plus, times, eq, gt) with array expansion (AX) enabled.

Download Now | Watch this File

File Information
Description

This function is a generalization of BSXFUN, provided as a builtin function in MATLAB R2007a and later releases. It is part of the ARRAYLAB toolbox, together with MULTIPROD (MATLAB Central, file #8773) and the Vector algebra toolbox (MATLAB Central file #8782).

BAXFUN is called by some of the functions in the vector algebra toolbox.

C = BAXFUN(FUNC, A, B) applies an element-by-element binary operation to arrays A and B, with singleton expansion (SX) enabled. It is equivalent to C = BSXFUN(FUNC, A, B). FUNC is a function handle.

C = BAXFUN(FUNC, A, B, SHIFTA, SHIFTB) applies an element-by-element binary operation to arrays A and B, with array expansion (AX) enabled. FUNC is a function handle. SHIFTA and SHIFTB specify the number of dimensions by which arrays A and B are optionally shifted to the right (positive values) or left (negative values). Thus,
C = BAXFUN(FUNC, A, B, SHIFTA, SHIFTB) is equivalent to
C = BAXFUN(FUNC, SHIFTDIM(A,-SHIFTA), SHIFTDIM(B,-SHIFTB)).
C = BAXFUN(FUNC, A, B, SHIFTA) can be used if B is not to be shifted.
 
FUNC can either be an handle for an M-function, or one of the following built-in-function handles:
 
@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 Division remainder
@mod Division modulus
@atan2 4-quadrant arc-tangent
@hypot SQRT of sum of squares
@eq Equal
@ne Not equal
@lt Less than
@le Less than or equal
@gt Greater than
@ge Greater than or equal
@and Logical AND
@or Logical OR
@xor Logical EXCLUSIVE OR

If an M-function handle is specified, the M-function must be able to accept as input either two column vectors of the same size, or one column vector and one scalar, and return as output a column vector of the same size as the input(s).
 
Array expansion (AX) is a powerful generalization of the concept of scalar expansion. Scalar expansion is the virtual replication or annihilation of a scalar which allows you to combine it, element by element, with an array X of any size (e.g. X+10, X*10, or []-10). Similarly, in BAXFUN, the purpose of AX is to virtually match the sizes of A and B, before FUNC is applied. Indeed, A and B can be scalars, vectors, matrices, or multi-dimensional arrays, and may have different sizes. Dimension matching is achieved by means of a dimension shift followed by a singleton expansion:
 
1) DIMENSION SHIFT
A dimension shift (see SHIFTIM) is applied to A or B if a non-zero value is specified for SHIFTA or SHIFTB. Notice that, in BAXFUN, a shift to the left is hardly ever needed. Hence, positive values of SHIFTA/B indicate a shift to the rigth, and negative values a shift to the left, whereas SHIFTDIM uses the opposite convention.
 
2) SINGLETON EXPANSION (SX)
Whenever a dimension of either A or B is singleton and the corresponding dimension of the other array is not, the mismatch is fixed by virtually replicating (or diminishing to length 0) the array along that dimension.
 
BAXFUN applies elementwise operations. Matrix multiplications with AX enabled can be performed using MULTIPROD (MATLAB Central, file #8773). BAXFUN calls the builtin function BBXFUN, available in MATLAB R2007a. For earlier MATLAB releases, use Schwarz's replacement of BSXFUN (MATLAB Central, file #23005)
 
Examples:
Subtracting the column means from a matrix A
  a = magic(5); % ................. 5×5
  a = baxfun(@minus, a, mean(a)); % 5×5
 
Subtracting the matrix means from 10 matrices contained in A
  a = rand(3, 3, 10); % ............... 3×3×10
  means = mean(reshape(a, 9, 10)); % .... 1×10
  a = baxfun(@minus, a, means, 0, 1); % 3×3×10
 
Multiplying matrix A by each element of B, i.e. multiplying each
element of A by each element of B (all possible combinations)
  a = [1 2 3 4; 5 6 7 8]; % ....... 2×4
  b = [1 10 100]; % ................. 1×3
  c = baxfun(@times, a, b, 0, 1); % 2×4×3
 
  c(:,:,1) =
      1 2 3 4
      5 6 7 8
 
  c(:,:,2) =
     10 20 30 40
     50 60 70 80
 
  c(:,:,3) =
    100 200 300 400
    500 600 700 800

Acknowledgements
This submission has inspired the following:
Vector algebra for arrays of any size, with array expansion enabled
MATLAB release MATLAB 7 (R14)
Other requirements BAXFUN calls the builtin function BBXFUN, available in MATLAB R2007a. For earlier MATLAB releases, use Schwarz's replacement of BSXFUN (MATLAB Central, file #23005)
Zip File Content  
Other Files baxfun.m,
testBAXFUN.m
Tags for This File  
Everyone's Tags
Tags I've Applied
Add New Tags Please login to tag files.
Comments and Ratings (1)
24 Feb 2009 Paolo de Leva

BAXFUN is just a trifling enhancement to the built-in function BSXFUN. The enhanced syntax just allows you to operate a dimension shift before applying the specified binary function. However, I believe it is useful to publish it for two reasons:
1) it teaches to whoever is willing to read the help text how much additional power you can obtain from BSXFUN if you combine it with a dimension shift (see examples), and it provides a compact syntax to do it with a single instruction.
2) AX was first enabled for matrix multiplication, with version 2.0 of MULTIPROD (MATLAB Central, file #8773), and for vectorial operations, with the latest version of the Vector algebra toolbox, (MATLAB Central file #8782). In these applications, it provides a terrific service. For the sake of consistency, however, I felt that it was also necessary to enable AX for element-wise operations.

Please login to add a comment or rating.
Tag Activity for this File
Tag Applied By Date/Time
array expansion Paolo de Leva 24 Feb 2009 11:36:11
bsxfun Paolo de Leva 24 Feb 2009 11:36:11
multidimensional array Paolo de Leva 25 Feb 2009 02:17:26
arraylab Paolo de Leva 25 Feb 2009 02:17:26
expansion Paolo de Leva 25 Feb 2009 02:17:26
matrix expansion Paolo de Leva 25 Feb 2009 02:17:26
array Paolo de Leva 25 Feb 2009 02:17:26
multiplication Paolo de Leva 25 Feb 2009 02:17:26
product Paolo de Leva 25 Feb 2009 02:17:26
replication Paolo de Leva 25 Feb 2009 02:17:26
singleton expansion Paolo de Leva 25 Feb 2009 02:17:26
virtual replication Paolo de Leva 25 Feb 2009 02:17:26
virtual expansion Paolo de Leva 25 Feb 2009 02:17:26
ldivide Paolo de Leva 25 Feb 2009 02:17:37
plus Paolo de Leva 25 Feb 2009 02:17:37
minus Paolo de Leva 25 Feb 2009 02:17:37
rdivide Paolo de Leva 25 Feb 2009 02:17:37
vector expansion Paolo de Leva 25 Feb 2009 02:17:37
times Paolo de Leva 25 Feb 2009 02:17:37
max Paolo de Leva 25 Feb 2009 02:17:37
xor Paolo de Leva 25 Feb 2009 02:17:37
power Paolo de Leva 25 Feb 2009 02:17:37
rem Paolo de Leva 25 Feb 2009 02:17:37
mod Paolo de Leva 25 Feb 2009 02:17:37
atan2 Paolo de Leva 25 Feb 2009 02:17:37
hypot Paolo de Leva 25 Feb 2009 02:17:37
eq Paolo de Leva 25 Feb 2009 02:17:37
lt Paolo de Leva 25 Feb 2009 02:17:37
gt Paolo de Leva 25 Feb 2009 02:17:37
ne Paolo de Leva 25 Feb 2009 02:17:37
le Paolo de Leva 25 Feb 2009 02:17:37
ge Paolo de Leva 25 Feb 2009 02:17:37
and Paolo de Leva 25 Feb 2009 02:17:37
or Paolo de Leva 25 Feb 2009 02:17:37
min Paolo de Leva 25 Feb 2009 02:17:37
baxfun Paolo de Leva 25 Feb 2009 02:17:41
boolean operation Paolo de Leva 28 Feb 2009 07:36:43
elementwise operation Paolo de Leva 28 Feb 2009 07:36:43
relational operation Paolo de Leva 28 Feb 2009 07:36:43
logical operation Paolo de Leva 28 Feb 2009 07:36:43
arithmetic operation Paolo de Leva 28 Feb 2009 07:36:43
 

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