-
Notifications
You must be signed in to change notification settings - Fork 5
/
bounded_array.m
executable file
·63 lines (59 loc) · 1.58 KB
/
bounded_array.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
function [x,ier]=bounded_array(x,lowerx,upperx)
% Modify array "x" in such a way that its entries do not exceed "upperx"
% and are not below "lowerx".
%
% Written by: E. Rietsch: October 21, 2006
% Last updated:
%
% [x,ier]=bounded_array(x,lowerx,upperx)
% INPUT
% x vector or matrix
% lowerx lower limit of the entries of "x"; either a constant or
% a matrix with the same dimension as "x"
% upperx upper limit of the entries of "x"; either a constant or
% a matrix with the same dimension as "x"
% OUTPUT
% x input matrix where entries < "lowerx" are replaced by "lowerx" and
% entries > "upperx" are replaced by "upperx".
% ier logical variable: "false" if no entry of "x" has been replaced and
% "true" otherwise
%
% EXAMPLE
% x0=randn(3,5);
% lowerx=-1;
% upperx=ones(3,5);
% [x,ier]=bounded_array(x0,lowerx,upperx);
% disp(x-x0)
% Error checking
dimsx=size(x);
if numel(lowerx) > 1
dimsl=size(lowerx);
if ~all(dimsx == dimsl)
error('Incompatible dimensions of "x" and "lowerx".')
end
end
if numel(upperx) > 1
dimsu=size(upperx);
if ~all(dimsx == dimsu)
error('Incompatible dimensions of "x" and "upperx".')
end
end
ier=false;
bool=x > upperx;
if any(bool(:))
if numel(upperx) == 1
x(bool)=upperx;
else
x(bool)=upperx(bool);
end
ier=true;
end
bool=x < lowerx;
if any(bool(:))
if numel(lowerx) == 1
x(bool)=lowerx;
else
x(bool)=lowerx(bool);
end
ier=true;
end