Feeding a vector to a cost function

5 visualizzazioni (ultimi 30 giorni)
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

Risposta accettata

Andrei Bobrov
Andrei Bobrov il 12 Dic 2011
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);

Più risposte (2)

David Young
David Young il 12 Dic 2011
How about
CostFunction(vector(:,1), vector(:,2))
though whether this is useful depends on what you want to achieve.

bym
bym il 12 Dic 2011
function f = CostFunction(vector) % brackets not necessary
x = vector(:,1);
y = vector(:,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);
end

Categorie

Scopri di più su Creating and Concatenating Matrices in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by