why vectorize function is not recommended?
Show older comments
can anyone explain to me in a simple way why vectorize function is not recommended, and how i can avoid any possible troubles from using it
and is there is a better way to do its functionality, please dont give me links, just explain to me in a simple way, thanks in advance.

12 Comments
Stephen23
on 28 Jan 2021
"why vectorize function is not recommended"
My guess is that it is because encoding functions as character vectors or inline functions is deprecated in favor of function handles. Most likely inline and vectorize will be obsoleted in a future release.
"how i can avoid any possible troubles from using it"
Don't use it. Either write vectorized code yourself, or use a loop / arrayfun.
Osama Alkurdi
on 28 Jan 2021
Walter Roberson
on 28 Jan 2021
If you already have a function handle then it should ideally have been written to vectorize it when written. vectorize is mostly for lazy writing, or for getting a function from a user who is not expected to know to vectorize. Admittedly, dealing with user provided functions is a challenge: it is not friendly to the user to require that they know the matlab operators.
Osama Alkurdi
on 28 Jan 2021
Walter Roberson
on 28 Jan 2021
try/catch is probably what you should use.
In theory better would be for you to define your own little programming language that the user was restricted to, and use a parser with the language, so that you can be sure that the user can only express valid expressions in the language... but that is a fair bit of work.
Stephen23
on 28 Jan 2021
It is probably simpler to assume that the user-provided function is not vectorized. Then just wrap it in a loop/arrayfun.
Osama Alkurdi
on 28 Jan 2021
Star Strider
on 28 Jan 2021
I actually like to use vectorize, specifically as:
s = 'x^2 + 2*x + 5'
f = str2func(['@(x)' vectorize(s)]);
when the original string expression was entered in inputdlg without being vectorized (or completely vectorized), and for long expressions in posts here that should be vectorized, and were posted without using vectorized operators.
I suppose it would be easy enough to write my own version of it, should it disappear.
I hope it survives!
@Star Strider: take a look at its code, you might be surprised at how simple it really is.
Can be trivially replaced with one regexprep call:
str = '*23.5*cat/in..../hat^99..^123';
regexprep(str,'(?<!\.)[*/^]','.$&')
vectorize(str)
(this is example is not supposed to be valid MATLAb syntax, it just shows that the outputs are the same)
Walter Roberson
on 28 Jan 2021
You missed one, Stephen, but it is quite obscure. The \ operator is vectorized as .\ also known as ldivide; https://www.mathworks.com/help/matlab/ref/ldivide.html
Also, long standing bug: vectorize changes quoted strings.
vectorize("ismember(A, {'+', '-', '*', '/', '^', '\'})")
"You missed one, Stephen, but it is quite obscure. The \ operator is vectorized as .\ also known as ldivide; "
No, as your own example clearly shows VECTORIZE() does not change that operator:
vectorize('a\b')
I did not "miss" that operator, because what I wrote replicates the behavior of VECTORIZE().
Answers (1)
Catalytic
on 29 Jan 2021
2 votes
A warning without an explanation is not a real warning. I say you just ignore it.
4 Comments
Rik
on 29 Jan 2021
Ignorance is bliss? Just because there is no link to an explanation that doesn't mean that there isn't a good reason to avoid it.
Catalytic
on 29 Jan 2021
Of course it does. If there were a good reason, why would it be omitted? And with no suggested alternatives, no less.
Matt J
on 29 Jan 2021
If no explanation implies the reason doesn't exist then, by the same logic, no explanation for the omission means the omission doesn't exist. So if the omission doesn't exist, maybe TMW feels than explanation was indeed given! :-)
Scroll to the bottom of the vectorize page and place a star rating which will allow you to type and submit feedback.
That feedback is received by MathWorks Gnomes which are never seen nor heard but visit the MathWorks documentation team at night while they are sleeping and whisper selected suggestions into their ears to manipulate their dreams. Occassionally this causes them to wake up with a Eureka moment that leads to changes in the documentation.

Categories
Find more on Scripts in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!