-
Notifications
You must be signed in to change notification settings - Fork 12
/
isbetween.m
144 lines (117 loc) · 4.13 KB
/
isbetween.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
function boolean = isbetween(a, b, c, varargin)
% Is "a" between values "b" and "c"?
%
% boolean = isbetween(a, b, c, ...
% 'inclusive', inclusive_switch, ...
% 'threshold', threshold_value])
%
% Example: isbetween(1, 0, 3) returns true.
% isbetween(1:4, 0, 3) returns [true, true, true, false]
% isbetween(1:4, 0, 3, false) returns [true, true, false, false]
%
% Inputs:
% a, b, c scalar or vector values
%
% Options:
% inclusive_switch Boolean: if a equals b or c, is it "between"?
% default: true (equals counts as "between")
% otherwise, specify false
% threshold_value Is there some small value for which overlaps
% should be ignored?
% Output:
% boolean true if a is between b & c
% Kevin J. Delaney
% November 19, 2008
% January 25, 2010 Extended to "b", "c" being vectors.
% March 10, 2010 Allow "a" to be a vector.
% April 5, 2010 Added 'inclusive', 'threshold' options.
% May 20, 2010 Return an output same orientation as input.
boolean = [];
inclusive = true;
threshold_value = 0;
if ~exist('a', 'var')
help(mfilename);
return
end
if ~isnumeric(a)
errordlg('Input "a" is non-numeric.', mfilename);
return
end
if ~exist('b', 'var') || ~isnumeric(b)
errordlg('Input "b" is missing or non-numeric.', mfilename);
return
end
if ~exist('c', 'var') || ~isnumeric(c)
errordlg('Input "c" is missing or non-numeric.', mfilename);
return
end
for index = 1 : 2 : (length(varargin) - 1)
option_name = varargin{index};
if isempty(option_name) || ~ischar(option_name)
errordlg('Option name is empty or non-char.', mfilename);
return
end
option_value = varargin{index + 1};
if isempty(option_value)
errordlg('Option value is empty.', mfilename);
return
end
switch lower(option_name)
case 'inclusive'
if ischar(option_value)
switch lower(option_value)
case {'inclusive', 'yes', 'true', 'y', 't'}
inclusive = true;
case {'exclusive', 'false', 'no', 'f', 'n'}
inclusive = false;
otherwise
errordlg(['Unknown "inclusive" selection "', option_value, '".'], ...
mfilename);
return
end
elseif islogical(option_value)
inclusive = option_value;
else
errordlg('Option "inclusive" neither boolean nor char.', mfilename);
return
end
case 'threshold'
if ~isnumeric(option_value)
errordlg('Input "threshold" is non-numeric.', mfilename);
return
end
threshold_value = option_value;
otherwise
errordlg(['Unknown option "', option_name, '".'], mfilename);
return
end
end
% Are b & c vectors?
num_tests = length(b);
if length(c) ~= num_tests
errordlg(['Length mismatch. There are ', num2str(length(b)), ...
' "b" values but ', num2str(length(c)), ' "c" values.'], mfilename);
return
end
% Make sure both are column vectors.
b = reshape(b, [num_tests, 1]);
c = reshape(c, [num_tests, 1]);
lower_limit = min([b, c], [], 2);
upper_limit = max([b, c], [], 2);
% if num_dims(a) > 1
% errordlg('Sorry--can''t accomodate "a" with more than one dimension.', ...
% mfilename);
% return
% end
boolean = false(num_tests, length(a));
for a_index = 1:length(a)
if inclusive
boolean(:, a_index) = (a(a_index) >= (lower_limit + threshold_value)) & (a(a_index) <= (upper_limit - threshold_value));
else
boolean(:, a_index) = (a(a_index) > (lower_limit + threshold_value)) & (a(a_index) < (upper_limit - threshold_value));
end
end
% Return an output same orientation as input.
if size(a, 1) > size(a, 2)
boolean = boolean';
end