<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8">
</head>
<body bgcolor="#FFFFFF" text="#000000">
<p>
</p>
<div class="moz-text-html" lang="x-unicode">
<p>Hi,</p>
<p>Looking for occurrences of a vector in an array is a general
task that can be useful in many situations. <br>
<br>
vectorfind() currently does it, but with some cumbersome
limitations.<br>
A work was started in last February to extend vectorfind()
capabilities.<br>
It was announced @
<a class="moz-txt-link-freetext"
href="http://mailinglists.scilab.org/Scilab-users-Search-for-a-subvector-in-a-vector-tp4035450p4035452.html">http://mailinglists.scilab.org/Scilab-users-Search-for-a-subvector-in-a-vector-tp4035450p4035452.html</a><font
face="Arial"><br>
</font><br>
An extended version is now available for tests and comments, at
the unchanged URL:<br>
<a class="moz-txt-link-freetext"
href="https://codereview.scilab.org/#/c/19055/">https://codereview.scilab.org/#/c/19055/</a><br>
A copy of its documentation page in PDF is attached.<br>
This is a Scilab Enhancement Proposal (SEP).<br>
<br>
Its history section summarizes new proposed features. <br>
They are reminded and illustrated herebelow.<br>
<br>
All comments are welcome.<br>
Best regards<br>
Samuel<br>
<br>
=======================<br>
</p>
<p><b>New features proposed</b>:<br>
</p>
<ul class="itemizedlist">
<li><code class="literal">vectorfind(H,[])</code> returns an
error. <br>
=> Returning <code class="literal">[]</code> is proposed
instead.<br>
<br>
</li>
<li>When the needle is longer than the haystack side size, an
error is yielded.<br>
=> Returning <code class="literal">[]</code> is proposed
instead<br>
<br>
</li>
<li>vectorfind() can't presently be used with a needle shorter
than the haystack size.<br>
This was the limitation pointed by Christophe in February.<br>
=> Supporting any <b>short needle</b> is proposed.<br>
example:<br>
<tt> m = </tt><tt>[</tt><tt><br>
</tt><tt> 1 0 0 1</tt><tt><br>
</tt><tt> 1 0 0 1</tt><tt><br>
</tt><tt> 0 0 1 0 ];<br>
vectorfind(m, [0 0], "r") // => </tt><tt>[3 4 5]
instead of an error<br>
</tt><tt>vectorfind(m, [1 1], "c") // => [1 10] instead of
an error</tt><br>
<br>
</li>
<li>When the haystack array has some <b>%nan</b> values, these
ones are presently never matching.<br>
=> Considering %nan values as regular ones is proposed.<br>
Example:<br>
<tt> m = </tt><tt>[</tt><tt><br>
</tt><tt> 1 %nan 0 1</tt><tt><br>
</tt><tt> 1 0 0 1</tt><tt><br>
</tt><tt> 0 0 1 %nan ];<br>
vectorfind(m, [1 %nan], "r") // => </tt><tt>[1 9]
instead of []<br>
</tt><br>
</li>
<li>It is presently not possible to use <b>wildcards</b> in the
vector to search. For instance, if in the previous matrix m we
search all columns starting with 0 and ending with 1, there is
no way to specify a [0 * * 1]-like vector as needle.</li>
<ul>
<li>A new <b><tt>joker</tt></b> option is proposed in input.
It declares a value that can be used in the vector and has
the status of a wildcard: It is matched by any value of the
haystack array.</li>
</ul>
<ul>
<li>In addition, a new <b>matching</b> output argument is
proposed. It allows to retrieve actual matching ranges.
Example with the m matrix hereabove:<br>
<tt>--> [ind, matching] = vectorfind(m, [1 </tt><tt><b>-1</b></tt><tt>
0], "r", </tt><tt><b>-1</b></tt><tt>)</tt><tt><br>
</tt><tt>ind = </tt><tt><br>
</tt><tt> 1. 2.</tt><tt><br>
</tt><tt>matching = </tt><tt><br>
</tt><tt> 1. Nan 0.</tt><tt><br>
</tt><tt> 1. 0. 0.</tt><tt><br>
</tt><br>
</li>
</ul>
<li>It is presently not possible to search the vector inside an
<b>hypermatrix</b>. Yet, N-Dimensional data are not rare --
like any RGB image --, and hypermatrices are now natively
encoded in Scilab 6. <br>
=> Supporting hypermatrices as haystack is proposed. The
attached page shows 2 examples, like selecting all pixels of a
RGB image (3D array) having some given [r g b] values.<br>
Another short example:<br>
<tt>--> m = grand(2,4,3,"uin",0,2)</tt><tt><br>
</tt><tt> m =</tt><tt><br>
</tt><tt>(:,:,1)</tt><tt><br>
</tt><tt> 2. 1. 2. 1.</tt><tt><br>
</tt><tt> 1. 1. 0. 0.</tt><tt><br>
</tt><tt>(:,:,2)</tt><tt><br>
</tt><tt> 0. 1. 0. 2.</tt><tt><br>
</tt><tt> 2. 0. 1. 0.</tt><tt><br>
</tt><tt>(:,:,3)</tt><tt><br>
</tt><tt> 0. 1. 2. 2.</tt><tt><br>
</tt><tt> 0. 0. 1. 2.</tt><tt><br>
</tt><tt><br>
</tt><tt>--> vectorfind(m, [2 0], "c")</tt><tt><br>
</tt><tt> ans =</tt><tt><br>
</tt><tt> 3. 8.</tt><tt><br>
</tt><tt><br>
</tt><tt>--> [ind, matching] = vectorfind(m, [0 -1 -1 2],
"r", -1)</tt><tt><br>
</tt><tt> matching = </tt><tt><br>
</tt><tt> 0. 1. 0. 2.</tt><tt><br>
</tt><tt> 0. 1. 2. 2.</tt><tt><br>
</tt><tt> 0. 0. 1. 2.</tt><tt><br>
</tt><tt> ind = </tt><tt><br>
</tt><tt> 3. 5. 6.</tt><br>
<br>
</li>
<li>Extending vectorfind() to hypermatrices requires to allow
probing extra directions beyond rows and columns. Hence,
specifying the <b>probing direction as an integer 1 , 2, 3</b>,..
is proposed. However, as indicated in the page (Description
section), the equivalences 1 <=> "r" and 2 <=> "c"
are questionable. The present proposal is implemented with
them.<br>
Example with the m hypermatrix hereabove:<br>
<tt>--> vectorfind(m, [2 0], 3)</tt><tt><br>
</tt><tt> ans =</tt><tt><br>
</tt><tt> 1. 5. 10.</tt><br>
<br>
</li>
<li>Interpreting and using "matrix-like" default indices
returned by vectorfind() when the haystack is an hypermatrix
may be hard and not handy. A new option <b><code
class="literal">indType</code></b> is proposed to allow
returning explicit or truly linear indices. It is proposed as
well when searching in a simple matrix.<br>
Back to the last example hereabove, we will get, instead of [1
5 10]:<br>
<tt>--> vectorfind(m, [2 0], 3,,"headIJK")</tt><tt><br>
</tt><tt> ans =</tt><tt><br>
</tt><tt> 1. 1. 1.</tt><tt><br>
</tt><tt> 1. 3. 1.</tt><tt><br>
</tt><tt> 2. 1. 2.</tt><tt><br>
</tt><br>
</li>
</ul>
<p><font face="Arial"><br>
</font></p>
</div>
</body>
</html>