why vectorize function is not recommended?

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

"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.
thank you, i will implement the vectorize code in my code, (it is a very simple code),
does the reason of being (not recommended) is this?
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.
yeah, i am writing an app designer program for my college numerical engineering methods course and the user have to input a function to calculate its roots, and i want to vectorize this function to be able to sketch a rough graph for the function so the user could locate the root nearby region, do you know how to check if the inputed function handle is valid, i tried try catch statement and it worked good for me, but is there is a better way to do it.
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.
It is probably simpler to assume that the user-provided function is not vectorized. Then just wrap it in a loop/arrayfun.
thank you guys,now i figured what i have to do.
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,'(?<!\.)[*/^]','.$&')
ans = '.*23.5.*cat./in..../hat.^99..^123'
vectorize(str)
ans = '.*23.5.*cat./in..../hat.^99..^123'
(this is example is not supposed to be valid MATLAb syntax, it just shows that the outputs are the same)
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, {'+', '-', '*', '/', '^', '\'})")
ans = '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')
ans = 'a\b'
I did not "miss" that operator, because what I wrote replicates the behavior of VECTORIZE().

Sign in to comment.

Answers (1)

A warning without an explanation is not a real warning. I say you just ignore it.

4 Comments

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.
Of course it does. If there were a good reason, why would it be omitted? And with no suggested alternatives, no less.
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! :-)
Adam Danz
Adam Danz on 29 Jan 2021
Edited: Adam Danz on 29 Jan 2021
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.

Sign in to comment.

Categories

Find more on Scripts in Help Center and File Exchange

Products

Release

R2020a

Asked:

on 28 Jan 2021

Edited:

on 15 Nov 2024

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!