Scilab Website | Contribute with GitLab | Mailing list archives | ATOMS toolboxes
Scilab Online Help
6.0.2 - 日本語

Change language to:
English - Français - Português - Русский

Please note that the recommended version of Scilab is 2024.0.0. This page might be outdated.
See the recommended documentation of this function

Scilabヘルプ >> Elementary Functions > setoperations > intersect

intersect

returns the unduplicated elements or rows or columns met in both input arrays

Syntax

M = intersect(a, b)
M = intersect(a, b, orient)
[M, ka] = intersect(..)
[M, ka, kb] = intersect(..)

Arguments

a, b

vectors, matrices or hypermatrices of encoded integers, decimal real numbers, or text. a and b must have the same datatype, but have independent sizes. For text inputs, UTF characters are accepted.

orient

flag with possible values : 1 or "r", 2 or "c". Can't be used if a or/and b is an hypermatrix.

M

matrix of the same datatype as a and b.

  • Without orient: M is a row vector.
  • With orient="r"|1: M is a matrix stacking the common rows of a and b.
  • With orient="c"|2: M is a matrix stacking the common columns of a and b.

ka

row vector of indices in a.

kb

row vector of indices in b.

Description

intersect(a,b) returns a row vector of unduplicated sorted values present in both a and b arrays.

Two NaN elements are always considered as different. So NaN or rows or columns having NaN will never be in the result M.

[M, ka, kb] = intersect(a,b) additionnaly returns the vectors ka and kb of indices in a and b of selected components firstly met, such that M=a(ka) and M=b(kb).

Common rows or columns

When the orient argument is provided, the comparison is performed between the rows of a and b -- each one being considered as a whole --, or between their columns.

intersect(a,b,"r") or intersect(a,b,1) will return the matrix of stacked unduplicated rows met in both a and b, sorted in lexicographic ascending order. If a and b don't have the same number of columns, [] is returned without comparing the values.

[M,ka,kb] = intersect(a,b,"r") additionnaly returns the vectors ka and kb of the minimal indices of common rows, respectively in a and b, such that M=a(ka,:) and M=b(kb,:).

intersect(a,b,"c") or intersect(a,b,2) does the same for columns.

Examples

A = grand(3, 3, "uin", 0, 9)
B = grand(2, 4, "uin", 0, 9)
intersect(A, B)
[N, ka, kb] = intersect(A,B);
ka, kb
--> A = grand(3, 3, "uin", 0, 9)
 A  =
   0.   6.   4.
   6.   6.   6.
   2.   7.   9.

--> B = grand(2, 4, "uin", 0, 9)
 B  =
   1.   8.   0.   2.
   6.   2.   2.   1.

--> intersect(A, B)
 ans  =
   0.   2.   6.

--> [N, ka, kb] = intersect(A,B);
--> ka, kb
 ka  =
   1.   3.   2.
 kb  =
   5.   4.   2.

In the above example, note that 6 is met four times in A, at indices [2 4 5 8]. Only the minimal index 2 is returned in ka. Same situation for 2 in B.

NaN values can never be in the result:

%nan == %nan
intersect([1 -2 %nan 3 6], [%nan 1:3])
--> %nan == %nan
 ans  =
  F

--> intersect([1 -2 %nan 3 6], [%nan 1:3])
 ans  =
   1.   3.

intersect() can also process some characters or some texts. Since Scilab is great with UTF characters, here is an example with some Arabic contents, getting characters present in both sentences:

A = strsplit("هو برنامج علمي كبير ""Scilab""")'
B = strsplit("فهو حر ومفتوح")'
intersect(A,B)
--> A = strsplit("هو برنامج علمي كبير ""Scilab""")'
 A  =
!ه  و     ب  ر  ن  ا  م  ج     ع  ل  م  ي     ك  ب  ي  ر     "  S  c  i  l  a  b  "  !

--> B = strsplit("فهو حر ومفتوح")'
 B  =
!ف  ه  و     ح  ر     و  م  ف  ت  و  ح  !

--> intersect(A,B)
 ans  =
!   ر  م  ه  و  !

Column-wise or Row-wise processing of two matrices: Here we process 2 matrices of signed 1-byte integers, and get the common columns:

A = int8(grand(3,5,"uin",0,1))
B = int8(grand(3,9,"uin",0,1))
[M,ka,kb] = intersect(A, B, "c");
M, ka, kb
--> A = int8(grand(3,5,"uin",0,1))
 A  =
  0  0  1  1  1
  0  0  1  1  0
  0  0  0  0  1

--> B = int8(grand(3,9,"uin",0,1))
 B  =
  1  0  1  1  1  0  1  1  1
  1  0  0  1  1  1  0  0  0
  1  0  1  0  1  1  1  0  0

--> [M,ka,kb] = intersect(A, B, "c");
--> M, ka, kb
 M  =
  0  1  1
  0  0  1
  0  1  0

 ka  =
   1.   5.   3.

 kb  =
   2.   3.   4.

See also

  • members — 配列の各要素または他の配列の行または列を数える(及び位置を調べる)
  • unique — ベクトルまたは行列のユニークなな要素を展開
  • gsort — クイックソートアルゴリズムによるソート
  • union — ベクトルの和集合要素を展開
Report an issue
<< setoperations setoperations setdiff >>

Copyright (c) 2022-2023 (Dassault Systèmes)
Copyright (c) 2017-2022 (ESI Group)
Copyright (c) 2011-2017 (Scilab Enterprises)
Copyright (c) 1989-2012 (INRIA)
Copyright (c) 1989-2007 (ENPC)
with contributors
Last updated:
Thu Feb 14 15:02:07 CET 2019