Code covered by the BSD License  

Highlights from
Embiggen

5.0

5.0 | 1 rating Rate this file 2 Downloads (last 30 days) File Size: 2.65 KB File ID: #27603

Embiggen

by Patrick Mineault

 

13 May 2010

Add (or multiply, divide, etc) a matrix A to a vector b with the simple syntax A + Embiggen(b)

| Watch this File

File Information
Description

Embiggen is a class that makes Matrix-vector operations easier by virtually matching the size of a vector b to the size of amatrix A. This allows for such code as:
 
  C = A + Embiggen(b)
 
Here A is a matrix (for example of size 100x50)
b is a vector (for example of size 100x1)
 
 And the equation is understood as (in index notation)
                  C_ij = A_ij + b_j
 
  Which is more readable than the alternatives:
 
  C = A + b*ones(1,size(A,2));
  C = A + repmat(b,1,size(A,2));
  C = bsxfun(@plus,A,b);
 
Under the hood the "big matrix B" is never actually created, as bsxfun is used; however Embiggen avoids the ackward reverse polish notation style of bsxfun .

A neat example:

  %Center and scale columns of a matrix
  A = randn(50,60);
  A = A - Embiggen(mean(A));
  A = A ./ Embiggen(std(A));

  mean(A) %vector of zeros
  std(A) %vector of ones
 
The following operators are implemented:

        A + Embiggen(b)
        A - Embiggen(b)
        A .* Embiggen(b)
        A ./ Embiggen(b)
        A .\ Embiggen(b)
  
  And the logical operations:
       
        A == Embiggen(b)
        A ~= Embiggen(b)
        A < Embiggen(b)
        A > Embiggen(b)
        A <= Embiggen(b)
        A >= Embiggen(b)
        A & Embiggen(b)
        A | Embiggen(b)
        xor(A, Embiggen(b))
 
As well as the functions:
        max(A,Embiggen(b))
        min(A,Embiggen(b))
        rem(A,Embiggen(b))
        mod(A,Embiggen(b))
        atan2(A,Embiggen(b))
        hypot(A,Embiggen(b))
 
Embiggen also works between any two multidimensional arrays A and B as long as each dimension of A and B is equal to each other, or equal to one.
 
Embiggen does not redefine any core Matlab classes, unlike other solutions (for example bsxops).

Acknowledgements

The author wishes to acknowledge the following in the creation of this submission:
bsxops

MATLAB release MATLAB 7.6 (R2008a)
Tags for This File  
Everyone's Tags
Tags I've Applied
Add New Tags Please login to tag files.
Comments and Ratings (1)
19 May 2010 David Young  
Please login to add a comment or rating.
Tag Activity for this File
Tag Applied By Date/Time
bsxfun Patrick Mineault 14 May 2010 09:25:35
elementwise Patrick Mineault 14 May 2010 09:25:35
expansion Patrick Mineault 14 May 2010 09:25:35
operator Patrick Mineault 14 May 2010 09:25:35

Contact us at files@mathworks.com