Feeding a vector to a cost function
4 次查看(过去 30 天)
显示 更早的评论
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 个评论
采纳的回答
Andrei Bobrov
2011-12-12
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);
更多回答(2 个)
David Young
2011-12-12
How about
CostFunction(vector(:,1), vector(:,2))
though whether this is useful depends on what you want to achieve.
bym
2011-12-12
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
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Creating and Concatenating Matrices 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!