elements or rows or columns met in both input arrays, without duplicates
M = intersect(a, b) M = intersect(a, b, orient) [M, ka] = intersect(..) [M, ka, kb] = intersect(..)
a and b must
have the same datatype.
For text inputs, UTF characters are accepted.
Sparse numeric or boolean matrices are accepted: Either a or
b or both a and b may
be sparse.
table and timeseries are also accepted:
a and b must have the same variable names.
a or/and b is an hypermatrix, table and timeseries.
matrix of the same datatype as a and b.
orient: M is a row vector.orient="r"|1: M is a
matrix stacking the common rows of a and
b.orient="c"|2: M is a
matrix stacking the common columns of a and
b.M is sparse as soon as either a or
b is sparse and none is empty.
M is table or timeseries if a and b are
table or timeseries
a.
b.
intersect(a,b) returns a row vector of unique values
present in both a and b arrays. Values are
sorted in increasing order
If a and b are tables or timeseries, then intersect
returns the rows present in both a and b. In the case of timeseries, the row times are taken into account unlike
the row names for tables.
![]() | 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) additionally 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).
In the case of tables and timeseries, M = a(ka,:) and M = b(kb, :).
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") additionally 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.
![]() | This option is not supported for table and timeseries. |
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 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""")'
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.
intersect() for booleans is mainly useful with the "r" or "c" option.
Here is an example with a sparse boolean matrix:
[F, T] = (%f, %t); A = [F F T F T F ; T F F T T T ; T T F T F F] B = [F T F T F F ; T F F F T F ; F T F F T F] [M,ka,kb] = intersect(A, sparse(B), "c"); issparse(M), full(M), ka, kb | ![]() | ![]() |
--> A = [F F T F T F ; T F F T T T ; T T F T F F] A = F F T F T F T F F T T T T T F T F F --> B = [F T F T F F ; T F F F T F ; F T F F T F] B = F T F T F F T F F F T F F T F F T F --> [M,ka,kb] = intersect(A, sparse(B), "c"); --> issparse(M), full(M), ka, kb ans = T ans = F F T T T F F T F ka = 6. 1. 3. kb = 1. 5. 4.
intersect() for duration and datetime:
A = hours([1 7 7 4 3 6 6]); B = hours([7 5 3 0]); [M, ka, kb] = intersect(A, B) A = datetime("today") + A; B = datetime("today") + B; [M, ka, kb] = intersect(A, B) | ![]() | ![]() |
intersect() for table:
A = table([1 7 7 4 3 6 6]', [1:7]'); B = table([7 5 3 0]', [2;2;5;5]); [M, ka, kb] = intersect(A, B) A = table([1;3;4;2], ["d";"c";"f";"h"], [%f;%t;%t; %f], "VariableNames", ["double", "string", "boolean"]); B = table([2;3;4;1], [%t;%t;%t;%t], ["d";"c";"f";"h"], "VariableNames", ["double", "boolean", "string"]); [M, ka, kb] = intersect(A, B) | ![]() | ![]() |
intersect() for timeseries:
A = timeseries(hours([1:10])', floor(rand(10,1)*10)); B = timeseries(hours(1:2:15)', floor(rand(8,1)*15)); [M, ka, kb] = intersect(A, B) | ![]() | ![]() |
| Version | Description |
| 6.1.0 | Complex numbers are now accepted. |
| 6.1.1 | Sparse matrices are now accepted (numbers or booleans). |
| 2026.0.0 | Support for duration, datetime, table and timeseries types added. |