Feeding a vector to a cost function

4 vues (au cours des 30 derniers jours)
Pourya saber
Pourya saber le 12 Déc 2011
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

Réponse acceptée

Andrei Bobrov
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);
  1 commentaire
Pourya saber
Pourya saber le 12 Déc 2011
Thank you andrei, works well.

Connectez-vous pour commenter.

Plus de réponses (2)

David Young
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.
  1 commentaire
Pourya saber
Pourya saber le 12 Déc 2011
Yes this one do work as well, thank you

Connectez-vous pour commenter.


bym
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

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!

Translated by