Feeding a vector to a cost function
4 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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
0 commentaires
Réponse acceptée
Andrei Bobrov
le 12 Déc 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);
Plus de réponses (2)
David Young
le 12 Déc 2011
How about
CostFunction(vector(:,1), vector(:,2))
though whether this is useful depends on what you want to achieve.
bym
le 12 Déc 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
Voir également
Catégories
En savoir plus sur Creating and Concatenating Matrices dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!