-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfast_sorted_mask.m
133 lines (115 loc) · 4.83 KB
/
fast_sorted_mask.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
function idxs=fast_sorted_mask(data,min_val,max_val)
%fast_sorted_mask - a fast replacement for masking of ordered vectors based on binary search
% DATA VECTOR MUST BE SORTED! THERE IS NO CHECK, IT WILL JUST RETURN THE WRONG ANSWER!!!
% gives a speedup (~100) for taking small slices of large (>1e6 elments) sorted vectors
% if the data is not already sorted then you can still get a net speedup
% by sorting the data then use this code upwards of 30 times
% see test_fast_sorted_mask for a number of speed comparisons
% this code calculates only when min_value<data<max_val and will not return min_value=data OR data=max_val
% Syntax: min_max_val_idx=fast_sorted_mask(data,min_val,max_val)
% Example:
% %show speedup, basic masking 19ms this code 0.5ms
% data=sort(rand(1e7,1));
% min_val=0.9;
% max_val=0.91;
% tic; mask= data>min_val & data<max_val;
% subdata1=sort(data(mask)); toc
% tic;mask_idx=fast_sorted_mask(data,min_val,max_val);
% subdata2=data(mask_idx(1):mask_idx(2)); toc
% isequal(subdata1,subdata2)
% Other m-files required: none
% Also See:test_fast_sorted_mask
% Subfunctions: binary_search_first_elm
% MAT-files required: none
%
% Known BUGS/ Possible Improvements
% -fix whatever is causeing execution time to flatten out in log-log past ~7e6. see test_fast_sorted_mask
% -should compile it for more speed!
% Author: Bryce Henson
% email: Bryce.Henson[a circle]live.com %YOU MUST INCLUDE '[matlab][fast_sorted_mask]' in the subject line OR I WILL NOT REPLY
% Last revision:2018-09-30
%------------- BEGIN CODE --------------
elms=numel(data);
max_idx=1;%initalize
if data(1)>min_val
min_idx=1;
if data(1)>=max_val
max_idx=min_idx-1;%no output
end
else
min_idx=binary_search_first_elm(data,min_val,1,elms);
if data(min_idx)<=min_val
min_idx=min_idx+1;
end
end
if data(end)<max_val
max_idx=elms;
if data(end)<=min_val
min_idx=max_idx+1;%no output
end
elseif max_idx~=0 %prevent calculating if both limits are blow the first element
max_idx=binary_search_first_elm(data,max_val,min_idx,elms);
if data(max_idx)>=max_val
max_idx=max_idx-1;
end
end
% if max_idx<min_idx
% warning('fast_sorted_mask:no data points in range')
% end
idxs=[min_idx,max_idx];
end
%modified from mathworks submission by Benjamin Bernard
%from https://au.mathworks.com/matlabcentral/fileexchange/37915-binary-search-for-closest-value-in-an-array
function idx_closest = binary_search_first_elm(vec, val,lower_idx,upper_idx)
% Returns index of vec that is closest to val, searching between min_idx start_idx .
%If several entries
% are equally close, return the first. Works fine up to machine error (e.g.
% [v, i] = closest_value([4.8, 5], 4.9) will return [5, 2], since in float
% representation 4.9 is strictly closer to 5 than 4.8).
% ===============
% Parameter list:
% ===============
% arr : increasingly ordered array
% val : scalar in R
% use for debug in loop %fprintf('%i, %i, %i\n',btm,top,mid)
top = upper_idx(1);
btm = lower_idx(1);
if top <= btm + 1 %deal with the case that would leave mid undefined
mid=top;
end
% Binary search for index
while top > btm + 1
mid = floor((top + btm)/2);
% Replace >= here with > to obtain the last index instead of the first.
if vec(mid) >= val
top = mid;
else
btm = mid;
end
end
idx_closest=mid;
end
% Copyright (c) 2012, Benjamin Bernard
% All rights reserved.
%
% Redistribution and use in source and binary forms, with or without
% modification, are permitted provided that the following conditions are
% met:
%
% * Redistributions of source code must retain the above copyright
% notice, this list of conditions and the following disclaimer.
% * Redistributions in binary form must reproduce the above copyright
% notice, this list of conditions and the following disclaimer in
% the documentation and/or other materials provided with the distribution
%
% THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
% AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
% IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
% ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
% LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
% CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
% SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
% INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
% CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
% ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
% POSSIBILITY OF SUCH DAMAGE.