实现边界控制的函数 via Matlab语言
%% 实现边界控制的函数 via Matlab语言
Lb = [0 0 0 0];
Ub = [8 7 6 5];
s = [10 3 4 9];
%%方法一 apply bounds
s = max(Lb,s);
s = min(Ub,s);
%%方法二 apply bounds
temp=s;
I=temp<Lb; % I=1×4 logical
temp(I) = Lb(I);
J = temp > Ub; % J=1×4 logical
temp(J) = Ub(J);
% Update this new move
s = temp;
%%方法三 apply bounds
A=Bounds( s, Lb, Ub)
function s = Bounds( s, Lb, Ub )
% Apply the lower bound vector
temp = s;
I = temp < Lb; % I=1×4 logical
temp(I) = Lb(I);
% Apply the upper bound vector
J = temp > Ub; % J=1×4 logical
temp(J) = Ub(J);
% Update this new move
s = temp;
end