MATLAB Answers

vectorization of anonymous function?

19 views (last 30 days)
Jan Valdman
Jan Valdman on 18 Dec 2016
Commented: Jan on 18 Dec 2016
I managed to vectorize the function below using inline command:
u_function=inline(vectorize('sin(x*y*z^2)'),'x','y','z') x=[1 2 3 4]; y=[3 4 3 4]; z=[1 2 4 4]; u=u_function(x,y,z)
How do I rewrite it in terms of an anonymous function?
Cheers, Jan

  0 Comments

Sign in to comment.

Answers (1)

Mischa Kim
Mischa Kim on 18 Dec 2016
Edited: Mischa Kim on 18 Dec 2016
Jan, is the resulting u(x,y,z) also a 1-by-4?
How about:
u = str2func(vectorize('@(x,y,z)sin(x*y*z^2)'));
x = [1 2 3 4]; y = [3 4 3 4]; z = [1 2 4 4];
u_eval = u(x,y,z)

  2 Comments

Jan
Jan on 18 Dec 2016
Move from Answer section (please pst comments in the comment section - Thanks)
Hi Mischa,
thank you, all vectors should have the size, yes. However, I am going to use more difficult forms of functions instead of x*y*z^2 and would like Matlab to put dots automatically (using vectorize?) within anonymous function. Is there a way to do it?
Jan
Jan on 18 Dec 2016
Mischa wrote: Jan, understood. Check out the updated answer above. Does this do the trick?

Sign in to comment.

Sign in to answer this question.


Translated by