MATLAB: Feeding a vector to a cost function

matlab function

Hello
I am wondering how I can feed a random (10,2) matrix to a cost function that get 2 inputs
My cost function is
function [f] = CostFunction(x,y)
f=4*(1-x).^2.*exp(-(x.^2)-(y+1).^2) -15*(x/5 - x.^3 - y.^5).*exp(-x.^2-y.^2) -(1/3)*exp(-(x+1).^2 - y.^2)-1*(2*(x-3).^7 -0.3*(y-4).^5+(y-3).^9).*exp(-(x-3).^2-(y-3).^2);
end
and I have created a random matrix like
vector=rand(10,2)
Now I want to be able to say something like this
CostFunction(Vector)
So I can get the value of Cost function for all those inputs
thank you

Best Answer

function [f] = CostFunction(v)
x = v(:,1);
y = v(:,2);
f=4*(1-x).^2.*exp(-(x.^2)-(y+1).^2) -15*(x/5 - x.^3 - y.^5).*exp(-x.^2-y.^2) -(1/3)*exp(-(x+1).^2 - y.^2)-1*(2*(x-3).^7 -0.3*(y-4).^5+(y-3).^9).*exp(-(x-3).^2-(y-3).^2);
solution
vectors = rand(10,2);
out = CostFunction(vectors);