File Exchange

image thumbnail

Generalized Array Operations

version 1.0.0.0 (2.63 KB) by Douglas Schwarz
Computes generalized array operations by expanding singleton dimensions.

0 Downloads

Updated 15 Mar 2006

View License

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

Comments and Ratings (4)

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

Emmanuel FARHI

Absolutely perfect !

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

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.

Updates

1.0.0.0

Modified so it works with a function returning logical.

fixed documentation typo

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

Inspired: bsxfun substitute

Discover Live Editor

Create scripts with code, output, and formatted text in a single executable document.


Learn About Live Editor