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
hi,

inspired by the question of Carlo Rossi:
http://www.nabble.com/cell-p23833329.html (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:
http://hg.savannah.gnu.org/hgweb/octave/rev/c0b104835d0d

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.

enjoy

--
RNDr. Jaroslav Hajek
computing expert & GNU Octave developer
Aeronautical Research and Test Institute (VZLU)
Prague, Czech Republic
url: www.highegg.matfyz.cz
_______________________________________________
Help-octave mailing list
[hidden email]
https://www-old.cae.wisc.edu/mailman/listinfo/help-octave