RunMat
GitHub

intersect — Return common elements or rows across arrays in MATLAB and RunMat.

intersect(A, B) computes the set intersection of A and B. Default ordering and optional index outputs follow MATLAB semantics.

Syntax

C = intersect(A, B)
C = intersect(A, B, option...)
[C, ia] = intersect(A, B)
[C, ia] = intersect(A, B, option...)
[C, ia, ib] = intersect(A, B)
[C, ia, ib] = intersect(A, B, option...)

Inputs

NameTypeRequiredDefaultDescription
AAnyYesFirst input array.
BAnyYesSecond input array.
optionStringScalarVariadicOption tokens: 'rows'|'sorted'|'stable'.

Returns

NameTypeDescription
CAnyIntersection values or rows.
iaNumericArrayIndices selecting matching elements/rows in A.
ibNumericArrayIndices selecting matching elements/rows in B.

Returned values from intersect depend on how many outputs the caller requests.

Errors

IdentifierWhenMessage
RunMat:intersect:LegacyOptionUnsupportedLegacy compatibility options are requested.intersect: the 'legacy' behaviour is not supported
RunMat:intersect:ConflictingOrderOptionsBoth 'sorted' and 'stable' options are provided.intersect: cannot combine 'sorted' with 'stable'
RunMat:intersect:UnknownOptionAn unsupported option token is provided.intersect: unrecognised option

How intersect works

  • intersect(A, B) flattens both inputs column-major, removes duplicates, and returns the sorted intersection.
  • [C, IA, IB] = intersect(A, B) also returns index vectors so that C = A(IA) and C = B(IB).
  • intersect(A, B, 'stable') preserves the first appearance order from A.
  • intersect(A, B, 'rows') treats each row as an element. Inputs must share the same number of columns.
  • Character arrays, string arrays, logical arrays, complex values, and numerical types are fully supported. Mixed classes raise a descriptive error.
  • Legacy flags such as 'legacy' or 'R2012a' are not supported in RunMat.

Does RunMat run intersect on the GPU?

intersect registers as a residency sink. When the active acceleration provider offers a dedicated intersect hook, the set logic can execute directly on the device and return GPU-resident results. Until such a hook lands, RunMat automatically gathers GPU tensors to host memory, runs the CPU implementation, and materializes host-side outputs so behavior matches MathWorks MATLAB exactly.

GPU memory and residency

You usually do not need to call gpuArray manually. RunMat's planner keeps tensors resident on the GPU when profitable. If a provider lacks an intersect kernel the runtime gathers automatically, so explicit residency management is rarely needed. Explicit calls to gpuArray remain available for compatibility with MathWorks MATLAB workflows.

Examples

Finding common values in numeric vectors

A = [5 7 5 1];
B = [7 1 3];
[C, IA, IB] = intersect(A, B)

Expected output:

C =
     1
     7
IA =
     4
     2
IB =
     2
     1

Preserving input order with 'stable'

A = [4 2 4 1 3];
B = [3 4 5 1];
C = intersect(A, B, 'stable')

Expected output:

C =
     4
     1
     3

Intersecting matrix rows

A = [1 2; 3 4; 1 2];
B = [2 3; 1 2];
[C, IA, IB] = intersect(A, B, 'rows')

Expected output:

C =
     1     2
IA =
     1
IB =
     2

Working with strings and character arrays

names1 = ["apple" "orange" "pear"];
names2 = ["pear" "grape" "orange"];
[common, IA, IB] = intersect(names1, names2, 'stable')

Expected output:

common =
  1×2 string array
    "orange"    "pear"
IA =
     2
     3
IB =
     3
     1

Using intersect on GPU arrays

G = gpuArray([10 4 6 4]);
H = gpuArray([6 4 2]);
result = intersect(G, H)

Expected output:

result =
     4
     6

Using intersect with coding agents

Open a RunMat example with live inputs, then ask the agent to explain how intersect changes the result.

Run a small intersect example, explain the result, then change one input and compare the output.

FAQ

Does intersect keep duplicate values?

No. MATLAB and RunMat return each common value at most once. Use other logic (such as logical indexing) if you need multiset behavior.

What is the default ordering?

intersect sorts results ascending by default. Specify 'stable' to preserve the input order from A.

Can I intersect rows that contain strings?

Yes. String arrays support both element and row intersections. When using 'rows', the inputs must have the same number of columns.

Are NaN values considered equal?

Yes. NaN values are treated as equal for the purposes of intersection, matching MATLAB.

Is the 'legacy' flag supported?

No. RunMat only implements the modern MATLAB semantics. Passing 'legacy' or 'R2012a' raises an error.

Sorting Sets

argsort · ismember · issorted · setdiff · sort · sortrows · union · unique

Shape

cat · circshift · diag · flip · fliplr · flipud · horzcat · ipermute · kron · permute · repelem · repmat · reshape · rot90 · squeeze · tril · triu · vertcat

Creation

colon · eye · false · fill · linspace · logspace · magic · meshgrid · ones · peaks · rand · randi · randn · randperm · range · true · zeros

Indexing

find · ind2sub · sub2ind

Introspection

isempty · ismatrix · isscalar · isvector · length · ndims · numel · size

Open-source implementation

Unlike proprietary runtimes, every RunMat function is open-source. Read exactly how intersect is executed, line by line, in Rust.

About RunMat

RunMat is an open-source runtime that executes MATLAB-syntax code blazing on any GPU. It is licensed under the Apache 2.0 license.

  • RunMat automatically optimizes your math for GPU execution on Apple, Nvidia, and AMD hardware. No code changes needed. Simulations that took hours now take minutes.
  • Start running code in seconds. RunMat runs in the browser, on the desktop, or from the CLI. No license server, no IT ticket.

Getting started · Benchmarks · Pricing

Download RunMat

Download RunMat for full performance, or use RunMat in your browser for zero setup.