vectorize

Adds element-wise operators to an expression string or inline function.

Category: Language Fundamentals > Matrices and Arrays

Syntax

s2 = vectorize(s1)

Returns the string s1 with *, / and ^ replaced by .*, ./ and .^.

g = vectorize(f)

Does the same for an inline function object f.

Description

Each *, / and ^ in the text gets a dot in front of it, unless it already has one. Evaluating the result on arrays then works element by element instead of performing matrix algebra.

It mainly serves older code that builds expressions from strings. With anonymous functions you simply write the dotted operators yourself.

Input arguments

s1
Character vector holding an expression.
f
Inline function object.

Output arguments

s2
The expression rewritten with element-wise operators.

Example

vectorize('x^2*y + 1/x')
Output
ans = 'x.^2.*y + 1./x'

See also

.* ./ .^ inline str2func