-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathqueryDiscretized1DFunction.m
69 lines (63 loc) · 2.11 KB
/
queryDiscretized1DFunction.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
function [ val ] = queryDiscretized1DFunction( x, f, inc, varargin )
% QUERYDISCRETIZED1DFUNCTION Evaluate a sampled function of one argument
%
% ## Syntax
% val = queryDiscretized1DFunction( x, f, inc [, offset ] )
%
% ## Description
% val = queryDiscretized1DFunction( x, f, inc [, offset ] )
% Returns the values of the discretized function that most closely
% correspond to the query values.
%
% ## Input Arguments
%
% x -- Query values
% An array (of any dimensions) containing the values at which to sample the
% function discretization.
%
% f -- Evaluated function
% A vector of length m representing the evaluation of a function at
% equally-spaced 1D points.
%
% inc -- Sample point spacing
% A scalar representing the (signed) constant spacing between the points
% at which the function was evaluated. For instance, the k-th element of
% `f` is equal to the evaluation of the function at a point `inc` units
% in the positive direction from the point at which the function was
% evaluated to produce the (k-1)-th element of `f`.
%
% offset -- Sampling axis origin
% The value at which the function was sampled to produce the first
% element of `f`.
%
% Defaults to zero if not passed.
%
% ## Output Arguments
%
% val -- Approximate function evaluation
% An array with the same dimensions as `x` containing the values from `f`
% corresponding to the closest points to the points in `x` at which the
% function was evaluated.
%
% ## Notes
% - For simplicity, this is a nearest-neighbour approach (i.e. zero-order
% interpolation of the discretized function).
% Bernard Llanos
% Spring 2016 research assistantship supervised by Dr. Y.H. Yang
% University of Alberta, Department of Computing Science
% File created August 9, 2016
nargoutchk(1, 1);
narginchk(3, 4);
if ~isempty(varargin)
offset = varargin{1};
else
offset = 0;
end
x_vector = x(:);
indices = floor(((x_vector - offset) / inc) + 0.5) + 1;
if any(indices < 1 | indices > length(f))
error('Some of the query values in input `x` are out of the domain sampled to construct input `f`.')
end
val = f(indices);
val = reshape(val, size(x));
end