How to use Arrayfun passing a random function as its arguments?

Hello. I kindly ask your help to solve the question below.
I am trying this:
func="sin";
R1=arrayfun(@sin,x1);
So, the question:
How I use "sin" as parameter without directly defining it in arrayfun?
Thank you very much.

 Accepted Answer

f=@sin;
R1=arrayfun(f,x1);

8 Comments

Hello Fangjun Jiang!
Thank you very much!
Can you help me another time(?) :
Suppose now I'd like to randomly choose between a list of functions: sin, cos, plus, minus, ... And these functions always acting on vectors. How it would be?
f='function that randomly choose between a list of functions';
R1=arrayfun(f,x1);
Thanks.
C = {@sin,@cos,@uplus,@uminus,@sqrt,...} % unary functions
fun = C{randi(numel(C))};
arrayfun(fun,...)
Note that this concept can be easily adapted for any set of functions that have the same number of input arguments.
Hello Stephen Cobeldick.
Thank you very much for helping.
"Note that this concept can be easily adapted for any set of functions that have the same number of input arguments." ---> That´s exactly what I am trying to do now.
The idea is to randomly choose between functions of 1, 2,...,n input argument(s) and then apply it to randomly choosen variables.
I am trying this:
B={x1,x2,x3,x4}; % column-vectors of numbers
var=B{randi(numel(B))};
C={@sin,@cos,@uplus,@uminus,@sqrt}; % unary functions
D={@plus,@minus,@times,@divide}; % bynary functions
fun1=C{randi(numel(C))};
fun2=D{randi(numel(D))};
fun=rand(fun1,fun2); % I am getting error here.
R=arrayfun(fun,var);
I am reading what you've suggested me:
https://www.mathworks.com/help/matlab/matlab_prog/creating-a-function-handle.html
@André Novaes: what do you expect this line to do?:
fun=rand(fun1,fun2);
It does not match any syntax listed in the rand help, nor what I showed you in my earlier comment. It is not clear from broken code what you want to achieve. Your code also shows only one input var to arrayfun:
R=arrayfun(fun,var);
How is this supposed to work with binary functions? For example, if you only provide one input variable to plus then you will get an error. If you are planning on randomly calling both unary and binary functions then you need to consider first the complexity of changing how many arguments you will have to provide: it would probably be easier to use an explicit loop of this, then you can use if or something similar.
I have tried this:
V={x1,x2,x3,x4};
var1=V{randi(numel(V))};
var2=V{randi(numel(V))};
F1={@sin,@cos,@uplus,@uminus,@sqrt}; % unary functions
F2={@plus,@minus,@times,@divide}; % bynary functions
fun1=F1{randi(numel(F1))};
fun2=F2{randi(numel(F2))};
R1=arrayfun(fun1,var1);
R2=arrayfun(fun2,var1,var2);
But I got the following error:
To use 'divide', the following product
must be licensed, installed, and enabled:
Fixed-Point Designer
Because
fun2 = @divide
@André Novaes: read the MATLAB documentation: numeric division is performed using one of ldivide, mldivide, rdivide, mrdivide, depending on what you want to achieve. Probably rdivide does what you want.
As the error message clearly states, divide is part of a the Fixed-Point Designer Toolbox, which is not likely to be very useful for you right now.
@Stephen Cobeldick thank you very much for helping!

Sign in to comment.

More Answers (0)

Tags

Community Treasure Hunt

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

Start Hunting!