search in ordered sets
[ind, occ, info] = dsearch(X, s )
[ind, occ, info] = dsearch(X, s , ch )
:X a mx-by-nx matrix of doubles, the entries to locate. : :s a n-by-1 or 1-by-n matrix of doubles, the intervals (if ch=”c”)
or the set (if ch=”d”). The values in s must be in strictly increasing order: s(1) < s(2) < ... < s(n)
:
This function locates the indices of the entries in X in the intervals or the set defined by s.
If ch=”c” (default), we consider the intervals I1 = [s(1), s(2)] and Ik = (s(k), s(k+1)] for k=2,...,n-1. Notice that the bounds of I1 are closed, while the left bounds of I2, ..., In are opened. For each entry X(i), we search the interval Ik which contains X(i), i.e. we search k such that s(k)<X(i)<=s(k+1).
More precisely,
: :occ(k) is the number of components of X which are in Ik : :info is the number of components of X which are not in any
interval Ik.
:
If ch=”d”, we consider the set {s(1),s(2),...,s(n)}. For each X(i), search k such that X(i)=s(k).
More precisely,
:ind(i) is k if X(i)=s(k) or 0 if X(i) is not in s. : :occ(k) is the number of entries in X equal to s(k) : :info is the number of entries in X which are not in the set
{s(1),...,s(n)}.
:
The ch=”c” option can be used to compute the empirical histogram of a function given a dataset. The ch=”d” option can be used to compute the entries in X which are present in the set s.
In the following example, we consider 3 intervals I1=[5,11], I2=(11,15] and I3=(15,20]. We are looking for the location of the entries of X=[11 13 1 7 5 2 9] in these intervals.
[ind, occ, info] = dsearch([11 13 1 7 5 2 9], [5 11 15 20])
The previous example produces the following output.
-->[ind, occ, info] = dsearch([11 13 1 7 5 2 9], [5 11 15 20])
info =
2.
occ =
4. 1. 0.
ind =
1. 2. 0. 1. 1. 0. 1.
We now explain these results.
In the following example, we consider the set [5 11 15 20] and are searching the location of the X entries in this set.
[ind, occ, info] = dsearch([11 13 1 7 5 2 9], [5 11 15 20],"d" )
The previous example produces the following output.
-->[ind, occ, info] = dsearch([11 13 1 7 5 2 9], [5 11 15 20],"d" )
info =
5.
occ =
1. 1. 0. 0.
ind =
2. 0. 0. 0. 1. 0. 0.
The following is a detailed explanation for the previous results.
The values in s must be in increasing order, whatever the value of the ch option. If this is not true, an error is generated, as in the following session.
-->dsearch([11 13 1 7 5 2 9], [2 1])
!--error 999
dsearch : the array s (arg 2) is not well ordered
-->dsearch([11 13 1 7 5 2 9], [2 1],"d")
!--error 999
dsearch : the array s (arg 2) is not well ordered
In the following example, we compare the empirical histogram of uniform random numbers in [0,1) with the uniform distribution function. To perform this comparison, we use the default search algorithm based on intervals (ch=”c”). We generate X as a collection of m=50 000 uniform random numbers in the range [0,1). We consider the n=10 values equally equally spaced values in the [0,1] range and consider the associated intervals. Then we count the number of entries in X which fall in the intervals: this is the empirical histogram of the uniform distribution function. The expectation for occ/m is equal to 1/(n-1).
m = 50000 ;
n = 10;
X = `grand`_(m,1,"def");
s = `linspace`_(0,1,n)';
[ind, occ] = dsearch(X, s);
e = 1/(n-1)*`ones`_(1,n-1);
`scf`_() ;
`plot`_(s(1:n-1), occ/m,"bo");
`plot`_(s(1:n-1), e,"r-");
`legend`_(["Experiment","Expectation"]);
`xtitle`_("Uniform random numbers","X","P(X)");
In the following example, we compare the histogram of binomially distributed random numbers with the binomial probability distribution function B(N,p), with N=8 and p=0.5. To perform this comparison, we use the discrete search algorithm based on a set (ch=”d”).
N = 8 ;
p = 0.5;
m = 50000;
X = `grand`_(m,1,"bin",N,p);
s = (0:N)';
[ind, occ] = dsearch(X, s, "d");
Pexp = occ/m;
Pexa = `binomial`_(p,N);
`scf`_() ;
`plot`_(s,Pexp,"bo");
`plot`_(s,Pexa',"r-");
`xtitle`_("Binomial distribution B(8,0.5)","X","P(X)");
`legend`_(["Experiment","Expectation"]);
In the following example, we use piecewise Hermite polynomials to interpolate a dataset.
// define Hermite base functions
function y=Ll(t, k, x)
// Lagrange left on Ik
y=(t-x(k+1))./(x(k)-x(k+1))
endfunction
function y=Lr(t, k, x)
// Lagrange right on Ik
y=(t-x(k))./(x(k+1)-x(k))
endfunction
function y=Hl(t, k, x)
y=(1-2*(t-x(k))./(x(k)-x(k+1))).*Ll(t,k,x).^2
endfunction
function y=Hr(t, k, x)
y=(1-2*(t-x(k+1))./(x(k+1)-x(k))).*Lr(t,k,x).^2
endfunction
function y=Kl(t, k, x)
y=(t-x(k)).*Ll(t,k,x).^2
endfunction
function y=Kr(t, k, x)
y=(t-x(k+1)).*Lr(t,k,x).^2
endfunction
x = [0 ; 0.2 ; 0.35 ; 0.5 ; 0.65 ; 0.8 ; 1];
y = [0 ; 0.1 ;-0.1 ; 0 ; 0.4 ;-0.1 ; 0];
d = [1 ; 0 ; 0 ; 1 ; 0 ; 0 ; -1];
X = `linspace`_(0, 1, 200)';
ind = dsearch(X, x);
// plot the curve
Y = y(ind).*Hl(X,ind) + y(ind+1).*Hr(X,ind) + d(ind).*Kl(X,ind) + d(ind+1).*Kr(X,ind);
`scf`_();
`plot`_(X,Y,"k-");
`plot`_(x,y,"bo")
`xtitle`_("Hermite piecewise polynomial");
`legend`_(["Polynomial","Data"]);
// NOTE : you can verify by adding these ones :
// YY = interp(X,x,y,d); plot2d(X,YY,3,"000")