4.66667

4.7 | 3 ratings Rate this file 102 downloads (last 30 days) File Size: 2.63 KB File ID: #10333

Generalized Array Operations

by Douglas Schwarz

 

13 Mar 2006 (Updated 15 Mar 2006)

Code covered by BSD License  

Computes generalized array operations by expanding singleton dimensions.

Download Now | Watch this File

File Information
Description

Computes functions of arrays where the singleton dimensions of each have been expanded to be the size of the other, without actually copying any data.

For example,

  x = rand(4,3,1,2);
  y = rand(4,1,5,2);
  z = genop(@times,x,y);

size(z) will be [4 3 5 2].

Acknowledgements
This submission has inspired the following:
bsxfun substitute
MATLAB release MATLAB 7.0.4 (R14SP2)
Tags for This File  
Everyone's Tags
Tags I've Applied
Add New Tags Please login to tag files.
Comments and Ratings (4)
14 Mar 2006 a user

Code gives error at line 75 when the function handle passed to the function returns a logical rather then a single, double, or integer.

15 Mar 2006 Douglas Schwarz

Comment by "A User" traces back to a limitation of the zeros function. I made changes to work around that and genop should work with logical functions now (as soon as the file gets updated).

23 Nov 2007 Emmanuel FARHI

Absolutely perfect !

27 Dec 2008 Paolo de Leva

An excellent "hack", a smart and computationally efficient algorithm. It is equivalent to BSXFUN (a builtin function introduced about a year later in MATLAB R2007a), and of course much slower, but it can be used as a replacement for BSXFUN by those who have earlier versions of MATLAB. This allowed me to ensure backward compatibility for version 2.0 of my MULTIPROD function, a generalization to N dimensions of the matrix product MTIMES (I am debugging it, I will publish it ass soon as it is ready):

http://www.mathworks.fr/matlabcentral/fileexchange/8773

MULTIPROD uses BSXFUN to implement singleton expansion and to increase computational speed.

There's another file in MATLAB Central File Exchange which does approximately the same job as GENOP (and perhaps more quickly), but does it less efficiently as for memory usage, by temporarily allocating a huge amount of additional memory to physically expand the arrays:

http://www.mathworks.com/matlabcentral/fileexchange/18685

Please login to add a comment or rating.
Updates
15 Mar 2006

fixed documentation typo

15 Mar 2006

Modified so it works with a function returning logical.

Tag Activity for this File
Tag Applied By Date/Time
matrices Douglas Schwarz 22 Oct 2008 08:18:19
array Douglas Schwarz 22 Oct 2008 08:18:20
repmat Douglas Schwarz 22 Oct 2008 08:18:20
matrix manipulation Douglas Schwarz 22 Oct 2008 08:18:20
singleton Douglas Schwarz 22 Oct 2008 08:18:20
generalized Douglas Schwarz 22 Oct 2008 08:18:20
bsxfun Paolo de Leva 27 Dec 2008 08:05:40
 

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