associative operators extended

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view

associative operators extended

Jaroslav Hajek-2

inspired by the question of Carlo Rossi: (and I think this appeared
several times before), I've extended the built-in functions `plus',
`times', `mtimes', `and', `or' to accept multiple arguments, in which
case they simply apply cumulatively from left to right:

This can be used to conveniently sum elements in a cell or struct array:

plus (matrices{:}) # or times/mtimes/and/or

the same can be achieved by using

sum (cat (3, matrices{:}), 3) # or prod/all/any, no equivalent for mtimes

Advantages of the former:
1. more readable
2. can handle a mixture of arbitrary matrix types and scalars, as well
as user classes.
3. expected to be more efficient (memory, probably time as well)

PS. Of course, if you work with a large number of equal-size
equal-type matrices, it is usually better to use a 3d array from the
very start, in which case the sum, prod, any, all reductions are the
most efficient.


RNDr. Jaroslav Hajek
computing expert & GNU Octave developer
Aeronautical Research and Test Institute (VZLU)
Prague, Czech Republic
Help-octave mailing list
[hidden email]