#!/usr/bin/python # Taken out of Matlab's help operators = """@plus Plus @minus Minus @times Array multiply @rdivide Right array divide @ldivide Left array divide @power Array power @max Binary maximum @min Binary minimum @rem Remainder after division @mod Modulus after division @atan2 Four-quadrant inverse tangent @hypot Square root of sum of squares @eq Equal @ne Not equal @lt Less than @le Less than or equal @gt Greater than @ge Greater than or equal @and Element-wise logical AND @or Element-wise logical OR @xor Logical EXCLUSIVE OR""" def make_bsxfun(name, op, help): """Create a bsx.m file which performs bsxfun(op, x, y) These routines provide slightly neater access to common binary operations on arrays than an explicit call to bsxfun with a function handle.""" fid = open("bsx%s.m" % name, 'wb') fid.write( """function z = bsx%s(x, y) %% bsx%s(x, y) = bsxfun(%s, x, y); %% Binary "%s" operator applied element-wise to x and y. %% Singleton dimensions are implicitly expanded to make x and y match in size. z = bsxfun(%s, x, y); """ % (name, name, op, help, op)) fid.close() for line in operators.splitlines(): (op, help) = line.split(None, 1) name = op[1:] make_bsxfun(name, op, help) # And an extra one I prefer: make_bsxfun('div', '@rdivide', 'Right (the usual ./) array divide')