if you don't mind, would you please help me that how can I have a function of x as follow:
this function calculate two values for x
function MOP2(x)
n=length(x);
z1=1-exp(sum((x-1/sqrt(n)).^2));
z2=1-exp(sum((x+1/sqrt(n)).^2));
z=[z1;z2];
return z
end
in main code I want to have
costfunc=F(x)
but I don't know it exists in Julia or not. in matlab we can have that as following
costfunc=@(x) MOP2(x)
is there any function like @ in Julia?
Thanks very much.
Yes, there is a syntax for that.
These are called anonymous functions (although you can assign them a name).
Here are a few ways to do this.
Here are a few usage examples.
Note that you do not need to create an anonymous function like
x -> MOP2(x). If a function takes another function, you can simply passMOP2instead of passingx -> MOP2(x). Here is an example withround.There is also the
dosyntax while passing functions as arguments.If you want to give a name to your anonymous function, you might as well define another function like
and use
costFunclater.If you want to call a function with another name you may write
if it is inside a function. Otherwise. in global scope, it is better to add
constbefore the assignment statement.This is important for type-stability reasons.