<?xml version="1.0" encoding="UTF-8"?>
<!--
 * Scilab ( https://www.scilab.org/ ) - This file is part of Scilab
 * Copyright (C) 2008 - INRIA
 * Copyright (C) 2012 - 2016 - Scilab Enterprises
 * Copyright (C) 2017 - 2019 - Samuel GOUGEON
 *
 * This file is hereby licensed under the terms of the GNU GPL v2.0,
 * pursuant to article 5.3.4 of the CeCILL v.2.1.
 * This file was originally licensed under the terms of the CeCILL v2.1,
 * and continues to be available under such terms.
 * For more information, see the COPYING file which you should have received
 * along with this program.
 *
 -->
<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="unique" xml:lang="ja">
    <refnamediv>
        <refname>unique</refname>
        <refpurpose>ベクトルまたは行列のユニークなな要素を展開</refpurpose>
    </refnamediv>
    <refsynopsisdiv>
        <title>呼び出し手順</title>
        <synopsis>
            [U, km, ku, nb] = unique(M)
            [U, km, ku, nb] = unique(M, orient)
            [U, km, ku, nb] = unique(.., "keepOrder")
            [U, km, ku, nb] = unique(.., "uniqueNan")
        </synopsis>
    </refsynopsisdiv>
    <refsection role="parameters">
        <title>引数</title>
        <variablelist>
            <varlistentry>
                <term>M</term>
                <listitem>
                    <para>
                        vector, matrix, or hypermatrix of booleans, numbers, or strings.
                        Numeric or boolean sparses are accepted.
                    </para>
                </listitem>
            </varlistentry>
            <varlistentry>
                <term>orient</term>
                <listitem>
                    <para>フラグで以下の値のどれかとなる : 1 または "r", 2 または "c".
                        It can't be used if <varname>M</varname> is an hypermatrix.
                    </para>
                </listitem>
            </varlistentry>
            <varlistentry>
                <term>U</term>
                <listitem>
                    <itemizedlist>
                        <listitem>
                            <para>
                              If <varname>orient</varname> is not used: Vector of extracted
                              <varname>M</varname> components sorted in ascending order.
                              If <varname>M</varname> is a row vector, <varname>U</varname> is also
                              a row vector. In all other <varname>M</varname> cases,
                              <varname>U</varname> is a column vector.
                            </para>
                        </listitem>
                        <listitem>
                          If <varname>orient</varname> is used: Matrix of extracted
                          <varname>M</varname> rows or columns, sorted in lexicographic ascending order.
                        </listitem>
                    </itemizedlist>
                    <para/>
                    If <varname>M</varname> is sparse, then <varname>U</varname> is always sparse.
                    <para/>
                </listitem>
            </varlistentry>
            <varlistentry>
                <term>km</term>
                <listitem>
                    Vector of indices of first encountered occurrences, such that
                    <literal>U = M(km)</literal> or <literal>U = M(km,:)</literal>
                    or <literal>U = M(:,km)</literal>.
                    <para/>
                    <varname>km</varname> is a row if <varname>M</varname> is a row or if
                    <literal>orient="c"</literal> is used. Otherwise it's a column.
                    <para/>
                </listitem>
            </varlistentry>
            <varlistentry>
                <term>ku</term>
                <listitem>
                    Array of indices in U, such that, according to the <varname>orient</varname> option
                    <itemizedlist>
                        <listitem>
                            "*": <varname>ku</varname> is of size size(M), and <literal>U(ku) = M</literal>
                        </listitem>
                        <listitem>
                            "r": <varname>ku</varname> is of size [size(M,1), 1], and <literal>U(ku,:) = M</literal>
                        </listitem>
                        <listitem>
                            "c": <varname>ku</varname> is of size [1, size(M,2)], and <literal>U(:,ku) = M</literal>
                        </listitem>
                    </itemizedlist>
                    <para/>
                    <warning>
                        <varname>ku</varname> is dense, even when <varname>M</varname> is sparse and the
                        overall "*" mode is used.
                    </warning>
                </listitem>
            </varlistentry>
            <varlistentry>
                <term>nb</term>
                <listitem>
                    <para>
                      Vector of integers > 0, with the same <varname>km</varname> shape:
                      Numbers of occurences in <varname>M</varname> of respective unduplicated
                      entities (components, rows, columns) returned in <varname>U</varname>.
                    </para>
                </listitem>
            </varlistentry>
        </variablelist>
    </refsection>
    <refsection role="description">
        <title>説明</title>
        <para>
            <literal>unique(M)</literal> は
            <literal>M</literal>のユニークなエントリを昇順に
            したものを保持するベクトルを返します.
        </para>
        <para>
            <literal>unique(M,"r")</literal> または
            <literal>unique(M,1)</literal>は,
            <literal>M</literal>のユニークな行を
            辞書式の昇順にして返します.
        </para>
        <para>
            <literal>unique(M,"c")</literal> または
            <literal>unique(M,2)</literal>は
            <literal>M</literal>のユニークな列を
            辞書式の昇順にして返します.
        </para>
        <para>
            <literal>unique(M,.. "keepOrder")</literal> returns <varname>M</varname> unduplicated
            entries in their original order in <varname>M</varname>.
            <literal>"keepOrder"</literal> is case-insensitive.
        </para>
        <para>
            <literal>unique(M,.. "uniqueNan")</literal> considers all Nan values as the same one,
            and unduplicates them. By default, any Nan is different
            from any other Nan, including itself: <literal>%nan&lt;>%nan</literal> is true, unless
            <literal>"uniqueNan"</literal> is used. Specifying
            <literal>"uniqueNan"</literal> is case-insensitive.
        </para>
        <para>
            For booleans, <literal>unique(…)</literal> is useful mainly with the "r" or "c" options.
        </para>
        <para>
            <note>
                Complex numbers are sorted first by increasing magnitudes, then by increasing
                phases on [-π,π].
            </note>
        </para>
    </refsection>
    <refsection role="examples">
        <title>例</title>
        <para>With some numbers:</para>
        <programlisting role="example"><![CDATA[
M = int8([2  0  2  2  1  1  1  2  1  1  0  1  1  0  1  1
          0  1  2  0  1  2  2  0  1  1  2  0  1  0  0  0
          ])
[u, km] = unique(M)
[uc, kmc] = unique(M, "c")
 ]]></programlisting>
    <screen><![CDATA[
--> M
 M =
  2  0  2  2  1  1  1  2  1  1  0  1  1  0  1  1
  0  1  2  0  1  2  2  0  1  1  2  0  1  0  0  0

--> [u, km] = unique(M)
 u  =
  0
  1
  2

 km  =
   2.
   4.
   1.

--> [uc, kmc] = unique(M, "c")
 uc  =
  0  0  0  1  1  1  2  2
  0  1  2  0  1  2  0  2

 kmc  =
   14.   2.   11.   12.   5.   6.   1.   3.
]]></screen>
        <para>With complex numbers:</para>
        <programlisting role="example"><![CDATA[
i = %i;
c = [1+i, 1-i, -i, i, -i, 1+i]
[u, k] = unique(c)
[uc, kc] = unique(c, "c")
 ]]></programlisting>
    <screen><![CDATA[
--> c = [1+i, 1-i, -i, i, -i, 1+i]
 c  =
   1. + i    1. - i    0. - i    0. + i    0. - i    1. + i

--> [u, k] = unique(c)
 u  =
   0. - i    0. + i    1. - i    1. + i
 k  =
   3.   4.   2.   1.

--> [uc, kc] = unique(c, "c")
 uc  =
   0. - i    0. + i    1. - i    1. + i
 kc  =
   3.   4.   2.   1.
]]></screen>

    <para>With some texts:</para>
        <programlisting role="example"><![CDATA[
t = ["BA" "BB" "AB" "BA" "AB" "BA" "AB" "AB" "BA" "AA" "AB" "BA" "BA" "BA" "AA"
     "AA" "AB" "AA" "AA" "BB" "BB" "BB" "BA" "AB" "AB" "BB" "BB" "AB" "AB" "AA"
    ]
u = unique(t)'
[u, kt, nb] = unique(t(1,:))
[u, kt] = unique(t(1,:), "keepOrder")      // In original order of row#1 elements
[uc, ktc, kuc, nb] = unique(t, "c")
[uc, ktc, kuc, nb] = unique(t, "c", "keepOrder")  // In original order of columns
[and(t(:,ktc)==uc), and(uc(:,kuc)==t) ]
 ]]></programlisting>
    <screen><![CDATA[
--> t = ["BA" "BB" "AB" "BA" "AB" "BA" "AB" "AB" "BA" "AA" "AB" "BA" "BA" "BA" "AA"
  >      "AA" "AB" "AA" "AA" "BB" "BB" "BB" "BA" "AB" "AB" "BB" "BB" "AB" "AB" "AA"
  >     ]
 t  =
!BA  BB  AB  BA  AB  BA  AB  AB  BA  AA  AB  BA  BA  BA  AA  !
!AA  AB  AA  AA  BB  BB  BB  BA  AB  AB  BB  BB  AB  AB  AA  !

--> u = unique(t)'
 u  =
!AA  AB  BA  BB  !

--> [u, kt, ku, nb] = unique(t(1,:))
 u  =
!AA  AB  BA  BB  !

 kt  =
   10.   3.   1.   2.

 nb  =
   2.   5.   7.   1.

--> [u, kt] = unique(t(1,:), "keepOrder")  // Keeping the original order
 u  =
!BA  BB  AB  AA  !

 kt  =
   1.   2.   3.   10.

--> [uc, ktc, kuc, nb] = unique(t, "c")
 uc  =
!AA  AA  AB  AB  AB  BA  BA  BA  BB  !             Sorted columns
!AA  AB  AA  BA  BB  AA  AB  BB  AB  !

 ktc  =
   15.   10.   3.   8.   5.   1.   9.   6.   2.

 nb  =
   1.   1.   1.   1.   3.   2.   3.   2.   1.

--> [uc, kc, nb] = unique(t, "c", "keepOrder")  // Keeping the original order
 uc  =
!BA  BB  AB  AB  BA  AB  BA  AA  AA  !
!AA  AB  AA  BB  BB  BA  AB  AB  AA  !

 kc  =
   1.   2.   3.   5.   6.   8.   9.   10.   15.

 nb  =
   2.   1.   1.   3.   2.   1.   3.   1.   1.

--> [and(t(:,ktc)==uc), and(uc(:,kuc)==t) ]
 ans  =
  T  T
]]></screen>
        <para/>
        <para>With Nan (and Inf) values. "uniqueNan" option:</para>
        <programlisting role="example"><![CDATA[
M = [2  2  %nan  1     2  0     1  %nan  0    %nan
     1  0  1     %nan  0  %inf  0  1     %inf 1
    ];
[v, km, kv, n] = unique(M); v',n'
[v, km, kv, n] = unique(M, "uniqueNan"); v',n'
unique(M, "c")
[v, kmc, kvc, n] = unique(M, "c", "uniqueNan")
 ]]></programlisting>
    <screen><![CDATA[
--> M
 M  =
   2.   2.   Nan   1.    2.   0.    1.   Nan   0.    Nan
   1.   0.   1.    Nan   0.   Inf   0.   1.    Inf   1.

--> [v, km, kv, n] = unique(M); v',n'
 ans  =
   0.   1.   2.   Inf   Nan   Nan   Nan   Nan

 ans  =
   5.   6.   3.   2.   1.   1.   1.   1.

--> [v, km, kv, n] = unique(M, "uniqueNan"); v',n'
 ans  =
   0.   1.   2.   Inf   Nan

 ans  =
   5.   6.   3.   2.   4.

--> unique(M, "c")
 ans  =
   0.    1.   1.    2.   2.   Nan   Nan   Nan
   Inf   0.   Nan   0.   1.   1.    1.    1.

--> [v, kmc, kvc, n] = unique(M, "c", "uniqueNan")
 v  =
   0.    1.   1.    2.   2.   Nan
   Inf   0.   Nan   0.   1.   1.

 kmc  =
   6.   7.   4.   2.   1.   3.

 n  =
   2.   1.   1.   2.   1.   3.
]]></screen>
    </refsection>
    <refsection role="see also">
        <title>参照</title>
        <simplelist type="inline">
            <member>
                <link linkend="members">members</link>
            </member>
            <member>
                <link linkend="gsort">gsort</link>
            </member>
            <member>
                <link linkend="vectorfind">vectorfind</link>
            </member>
            <member>
                <link linkend="grep">grep</link>
            </member>
            <member>
                <link linkend="union">union</link>
            </member>
            <member>
                <link linkend="intersect">intersect</link>
            </member>
        </simplelist>
    </refsection>
    <refsection role="history">
        <title>履歴</title>
        <revhistory>
            <revision>
                <revnumber>6.0.2</revnumber>
                <revdescription>
                    unique() can now be used to unduplicate complex numbers.
                </revdescription>
            </revision>
            <revision>
                <revnumber>6.1.0</revnumber>
                <revdescription>
                    <itemizedlist>
                        <listitem>
                            Extension to booleans.
                        </listitem>
                        <listitem>
                            "keepOrder" and "uniqueNan" options introduced.
                        </listitem>
                        <listitem>
                            Fourth output argument <literal>nb</literal> introduced.
                        </listitem>
                    </itemizedlist>
                </revdescription>
            </revision>
            <revision>
                <revnumber>6.1.1</revnumber>
                <revdescription>
                    ku 3rd output implemented. Sparse 2D matrices are now accepted.
                </revdescription>
            </revision>
        </revhistory>
    </refsection>
</refentry>