jeudi 22 septembre 2016

Matlab: How to compare the progressive value of a variable to it's previous best and reassign it to it's previous best?

I've a piece of code to evaluate the performance of a variable in a loop such that we keep check of its progress and compare it with it's best value ever and assign it back to this best value if the newly assigned value is evaluated to be not better than the performance of this variable at its best value.

for (True) 

err = sum(Y*X'*w <=0);   %measure the performance of w for fixed X and Y variables.
err_best = sum(Y*X'*wbest <=0);  %measure the performance of w best
if(err <= err_best)              %if performance of newly assigned value of w is better than that of its career best wbest then update wbest with this newly identified best value.
    wbest = w;            %update the value of wbest if the w is evaluated to be better than the previous best wbest.
end
w = wbest;                 % wbest is updated to the best value evaluated thus far and assign to w this value.

% Code to change w value.

end

Please ignore the rest of the code as that is immaterial for this question. I would like to know how to retain the best of the values of the variable w and store it in wbest as the w variable goes through code that changes it over and over. As of now, this piece of code doesn't give the results as expected and the rest of the code doesn't need any change.

Aucun commentaire:

Enregistrer un commentaire