how to choose the best value of a function

v=[1,1,1,0,0,0,0,0,0,0;0,0,0,1,1,1,0,0,0;0,0,0,0,0,0,0,1,1,1];
v1=perm(v);
for a=1:10;
for i=1:3
G=G=sum(h(a,i)+h(i,a))*v -sum(h(a,i)+h(i,a))*v1
Gain=G(v1,v,M);
disp(Gain),
end
end
I need to have the value of Gain of every permutation and after choose the permutation which have the least Gain value. Thank you for helping me

 Accepted Answer

This is standard programming stuff. Have variables called best_a, best_i, and best_gain. Then whenever the Gain is less than best_gain, store the current values in the best values. Something like:
v=[1,1,1,0,0,0,0,0,0,0;0,0,0,1,1,1,0,0,0;0,0,0,0,0,0,0,1,1,1];
v1=perm(v);
best_gain = inf;
for a=1:10;
for i=1:3
% What the heck is the next line!?!?!?!
G=G=sum(h(a,i)+h(i,a))*v -sum(h(a,i)+h(i,a))*v1
Gain(i, a) = G(v1,v,M);
disp(Gain(i, a));
% Save parameters if this gain is better.
if Gain(i, a) < best_gain
best_i = i;
best_a = a;
best_gain = Gain(i, a);
end
end
Or you could do it all at the end with the find() function:
[best_i best_a] = find(Gain == min(Gain(:));
best_gain = Gain(best_i, best_a);
If you do that, then take the if statement out of the loop.

More Answers (0)

Categories

Community Treasure Hunt

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

Start Hunting!