1 <?xml version="1.0" encoding="utf-8"?>
3 * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
4 * Copyright (C) Bruno Pincon
5 * Copyright (C) 2011 - DIGITEO - Michael Baudin
7 * This file must be used under the terms of the CeCILL.
8 * This source file is licensed as described in the file COPYING, which
9 * you should have received as part of this distribution. The terms
10 * are also available at
11 * http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt
14 <refentry xmlns="http://docbook.org/ns/docbook" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:svg="http://www.w3.org/2000/svg" xmlns:ns5="http://www.w3.org/1999/xhtml" xmlns:mml="http://www.w3.org/1998/Math/MathML" xmlns:db="http://docbook.org/ns/docbook" xmlns:scilab="http://www.scilab.org" xml:id="dsearch" xml:lang="en">
16 <refname>dsearch</refname>
18 search in ordered sets
22 <title>Calling Sequence</title>
24 [ind, occ, info] = dsearch(X, s )
25 [ind, occ, info] = dsearch(X, s , ch )
29 <title>Arguments</title>
35 a mx-by-nx matrix of doubles, the entries to locate.
43 a n-by-1 or 1-by-n matrix of doubles, the
44 intervals (if ch="c") or the set (if ch="d").
45 The values in s must be in strictly increasing order:
46 s(1) < s(2) < ... < s(n)
54 a 1-by-1 matrix of strings, the type of search (default ch="c").
55 Available values are ch="c" or ch="d".
63 a mx-by-nx matrix of doubles.
64 The location of the X values in the intervals or the set defined by <literal>s</literal>.
72 If ch="c", a (n-1)-by-1 or 1-by-(n-1) of doubles.
73 If ch="d", a n-by-1 or 1-by-n of doubles. The number of entries
74 from X in the intervals s.
82 a 1-by-1 matrix of doubles. The number of X entries which are not in <literal>s</literal>.
89 <title>Description</title>
91 This function locates the indices of the entries in X in the intervals or the set defined by s.
94 If <literal>ch="c"</literal> (default), we consider the
95 intervals <literal>I1 = [s(1), s(2)]</literal>
96 and <literal>Ik = (s(k), s(k+1)]</literal> for <literal>k=2,...,n-1</literal>.
97 Notice that the bounds of <literal>I1</literal> are closed,
98 while the left bounds of <literal>I2</literal>, ..., <literal>In</literal> are
100 For each entry <literal>X(i)</literal>,
101 we search the interval <literal>Ik</literal> which contains X(i), i.e. we search k such
102 that <literal>s(k)<X(i)<=s(k+1)</literal>.
112 is k such that <literal>Ik</literal> contains X(i) or
113 0 if X(i) is not in any interval <literal>Ik</literal>.
121 is the number of components of <literal>X</literal> which are in <literal>Ik</literal>
129 is the number of components of <literal>X</literal> which are not in
130 any interval <literal>Ik</literal>.
136 If <literal>ch="d"</literal>, we consider the set {s(1),s(2),...,s(n)}.
137 For each X(i), search k such that X(i)=s(k).
147 is k if X(i)=s(k) or 0 if X(i) is not in s.
155 is the number of entries in X equal to s(k)
163 is the number of entries in X which are not in the set
170 The ch="c" option can be used to compute the empirical histogram of a function given a dataset.
171 The ch="d" option can be used to compute the entries in X which are present in the set s.
175 <title>Examples</title>
177 In the following example, we consider 3 intervals <literal>I1=[5,11]</literal>,
178 <literal>I2=(11,15]</literal> and <literal>I3=(15,20]</literal>.
179 We are looking for the location of the entries of <literal>X=[11 13 1 7 5 2 9]</literal>
182 <programlisting role="example"><![CDATA[
183 [ind, occ, info] = dsearch([11 13 1 7 5 2 9], [5 11 15 20])
186 The previous example produces the following output.
189 -->[ind, occ, info] = dsearch([11 13 1 7 5 2 9], [5 11 15 20])
198 We now explain these results.
202 <para>X(1)=11 is in the interval I1, hence ind(1)=1.</para>
205 <para> X(2)=13 is in the interval I2, hence ind(2)=2.</para>
208 <para> X(3)=1 is not in any interval, hence ind(3)=0.</para>
211 <para> X(4)=7 is in the interval I1, hence ind(4)=1.</para>
214 <para> X(5)=5 is in the interval I1, hence ind(5)=1.</para>
217 <para> X(6)=2 is not in any interval, hence ind(6)=0.</para>
220 <para> X(7)=9 is in the interval I1, hence ind(7)=1.</para>
223 <para> There are four X entries (5, 7, 9 and 11) in I1, hence occ(1)=4.</para>
226 <para> There is one X entry (i.e. 13) in I2, hence occ(2)=1.</para>
229 <para> There is no X entry in I3, hence occ(3)=0.</para>
232 <para> There are two X entries (i.e. 1, 2) which are not in any interval, hence info=2.</para>
236 In the following example, we consider the set [5 11 15 20] and
237 are searching the location of the X entries in this set.
239 <programlisting role="example"><![CDATA[
240 [ind, occ, info] = dsearch([11 13 1 7 5 2 9], [5 11 15 20],"d" )
243 The previous example produces the following output.
246 -->[ind, occ, info] = dsearch([11 13 1 7 5 2 9], [5 11 15 20],"d" )
255 The following is a detailed explanation for the previous results.
260 X(1)=11 is in the set <literal>s</literal> at position #2, hence ind(1)=2.
265 X(2)=13 is not in the set <literal>s</literal>, hence ind(2)=0.
270 X(3)=1 is not in the set <literal>s</literal>, hence ind(3)=0.
275 X(4)=7 is not in the set <literal>s</literal>, hence ind(4)=0.
280 X(5)=5 is in the set <literal>s</literal> at position #1, hence ind(5)=1.
285 X(6)=2 is not in the set <literal>s</literal>, hence ind(6)=0.
290 X(7)=9 is not in the set <literal>s</literal>, hence ind(7)=0.
295 There is one entry X (i.e. 5) equal to 5, hence occ(1)=1.
300 There is one entry X (i.e. 11) equal to 1, hence occ(2)=1.
305 There are no entries matching <literal>s(3)</literal>, hence occ(3)=0.
310 There are no entries matching <literal>s(4)</literal>, hence occ(4)=0.
315 There are five X entries (i.e. 13, 1, 7, 2, 9) which are not in the set <literal>s</literal>, hence info=5.
320 The values in <literal>s</literal> must be in increasing order, whatever the
321 value of the <literal>ch</literal> option.
322 If this is not true, an error is generated, as in the following session.
325 -->dsearch([11 13 1 7 5 2 9], [2 1])
327 dsearch : the array s (arg 2) is not well ordered
328 -->dsearch([11 13 1 7 5 2 9], [2 1],"d")
330 dsearch : the array s (arg 2) is not well ordered
334 <title>Advanced Examples</title>
336 In the following example, we compare the empirical histogram of uniform
337 random numbers in [0,1) with the uniform distribution function.
338 To perform this comparison, we use the default search algorithm based on
340 We generate X as a collection of m=50 000 uniform random numbers in
342 We consider the n=10 values equally equally spaced values in the [0,1] range and
343 consider the associated intervals.
344 Then we count the number of entries in X which fall in the intervals:
345 this is the empirical histogram of the uniform distribution function.
346 The expectation for occ/m is equal to 1/(n-1).
348 <programlisting role="example"><![CDATA[
351 X = grand(m,1,"def");
352 s = linspace(0,1,n)';
353 [ind, occ] = dsearch(X, s);
354 e = 1/(n-1)*ones(1,n-1);
356 plot(s(1:n-1), occ/m,"bo");
357 plot(s(1:n-1), e,"r-");
358 legend(["Experiment","Expectation"]);
359 xtitle("Uniform random numbers","X","P(X)");
364 X = grand(m,1,"def");
365 s = linspace(0,1,n)';
366 [ind, occ] = dsearch(X, s);
367 e = 1/(n-1)*ones(1,n-1);
369 plot(s(1:n-1), occ/m,"bo");
370 plot(s(1:n-1), e,"r-");
371 legend(["Experiment","Expectation"]);
372 xtitle("Uniform random numbers","X","P(X)");
375 In the following example, we compare the histogram of binomially distributed
376 random numbers with the binomial probability distribution function B(N,p),
378 To perform this comparison, we use the discrete search algorithm based on
381 <programlisting role="example"><![CDATA[
385 X = grand(m,1,"bin",N,p);
387 [ind, occ] = dsearch(X, s, "d");
389 Pexa = binomial(p,N);
393 xtitle("Binomial distribution B(8,0.5)","X","P(X)");
394 legend(["Experiment","Expectation"]);
400 X = grand(m,1,"bin",N,p);
402 [ind, occ] = dsearch(X, s, "d");
404 Pexa = binomial(p,N);
408 xtitle("Binomial distribution B(8,0.5)","X","P(X)");
409 legend(["Experiment","Expectation"]);
412 In the following example, we use piecewise Hermite polynomials to
413 interpolate a dataset.
415 <programlisting role="example"><![CDATA[
417 // define Hermite base functions
419 // Lagrange left on Ik
420 y=(t-x(k+1))./(x(k)-x(k+1))
423 // Lagrange right on Ik
424 y=(t-x(k))./(x(k+1)-x(k))
427 y=(1-2*(t-x(k))./(x(k)-x(k+1))).*Ll(t,k,x).^2
430 y=(1-2*(t-x(k+1))./(x(k+1)-x(k))).*Lr(t,k,x).^2
433 y=(t-x(k)).*Ll(t,k,x).^2
436 y=(t-x(k+1)).*Lr(t,k,x).^2
439 x = [0 ; 0.2 ; 0.35 ; 0.5 ; 0.65 ; 0.8 ; 1];
440 y = [0 ; 0.1 ;-0.1 ; 0 ; 0.4 ;-0.1 ; 0];
441 d = [1 ; 0 ; 0 ; 1 ; 0 ; 0 ; -1];
442 X = linspace(0, 1, 200)';
446 Y = y(ind).*Hl(X,ind) + y(ind+1).*Hr(X,ind) + d(ind).*Kl(X,ind) + d(ind+1).*Kr(X,ind);
450 xtitle("Hermite piecewise polynomial");
451 legend(["Polynomial","Data"]);
452 // NOTE : it can be verified by adding these ones :
453 // YY = interp(X,x,y,d); plot2d(X,YY,3,"000")
458 y=(t-x(k+1))./(x(k)-x(k+1))
461 y=(t-x(k))./(x(k+1)-x(k))
464 y=(1-2*(t-x(k))./(x(k)-x(k+1))).*Ll(t,k,x).^2
467 y=(1-2*(t-x(k+1))./(x(k+1)-x(k))).*Lr(t,k,x).^2
470 y=(t-x(k)).*Ll(t,k,x).^2
473 y=(t-x(k+1)).*Lr(t,k,x).^2
476 x = [0 ; 0.2 ; 0.35 ; 0.5 ; 0.65 ; 0.8 ; 1];
477 y = [0 ; 0.1 ;-0.1 ; 0 ; 0.4 ;-0.1 ; 0];
478 d = [1 ; 0 ; 0 ; 1 ; 0 ; 0 ; -1];
479 X = linspace(0, 1, 200)';
482 Y = y(ind).*Hl(X,ind) + y(ind+1).*Hr(X,ind) + d(ind).*Kl(X,ind) + d(ind+1).*Kr(X,ind);
486 xtitle("Hermite piecewise polynomial");
487 legend(["Polynomial","Data"]);
490 <refsection role="see also">
491 <title>See Also</title>
492 <simplelist type="inline">
494 <link linkend="find">find</link>
497 <link linkend="tabul">tabul</link>