-
Notifications
You must be signed in to change notification settings - Fork 0
/
find_index.m
32 lines (31 loc) · 928 Bytes
/
find_index.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
function ind = find_index(X, vals)
%--------------------------------------------------------------------------
% find_index.m - Given a vector X and a set of values v, returns the
% indices of the values in X that most closely matches the values in vals.
%
% Usage: ind = find_index(X, val);
%
% Input: X * vector of values
% val * vector value to match
% Output: ind * index location of (closest) val in X
%
% Written by Marshall Crumiller
%--------------------------------------------------------------------------
% note: this shit is sexy
%{
lenX=length(X); lenvals = length(vals);
vals=repmat(vals',1,lenX);
X=repmat(X,lenvals,1);
d=abs(X-vals);
m=min(d,[],2);
m=repmat(m,1,lenX);
z=~(d-m);
[I,J] = find(z);
[~,m,~] = unique(I,'first');
ind=J(m);
%}
ind=zeros(1,length(vals));
for i = 1:length(vals)
d=abs(X-vals(i));
ind(i)=find(d==min(d),1,'first');
end