intrsect

Purpose

Returns the intersection of two vectors, with duplicates removed.

Format

y = intrsect(v1, v2[, flag])
Parameters:
  • v1 (Nx1 vector or string array) – data

  • v2 (Mx1 vector or string array) –

    data

    Note

    v2 must be the same type as v1

  • flag (scalar) – Optional argument, if 1, v1 and v2 are numeric; if 0, character. Default is flag equal to 1 (numeric).

Returns:

y (Lx1 vector) – all unique values that are in both v1 and v2 sorted in ascending order.

Examples

Basic usage, numeric

// Subject ID's from study 'a'
id_a = { 3758,
         3773,
         2615,
         2511 };

// Subject ID's from study 'b'
id_b = { 3779,
         3773,
         2001,
         3758,
         1585,
         2511 };

// Find the ID's that are in both groups
id_common = intrsect(id_a, id_b);

After the code above, id_common is equal to:

2511
3758
3773

Basic usage, string array

/*
** Variable names from dataset 'a'
** Create string array with the string
** vertical concatenation operator ($|)
*/
names_a = "oil" $| "copper" $| "silver" $| "cocoa";

// Variable names from dataset 'b'
names_b = "oil" $| "coffee" $| "cocoa" $| "tea";

// Find the variable names that are in both groups
names_common = intrsect(names_a, names_b);

After the code above, names_common is equal to:

cocoa
  oil

Character vectors

A character vector is different from a string array. A character vector is up to eight characters inside of the element of a numeric matrix.

/*
** Variable names from dataset 'a'
** Create character vector array with the
** numeric vertical concatenation operator (|)
*/
names_a = "oil" | "copper" | "silver" | "cocoa";

// Variable names from dataset 'b'
names_b = "oil" | "coffee" | "cocoa" | "tea";

// Set flag to tell 'intrsectsa' to treat input as character data
flag = 0;

// Find the variable names that are in both groups
names_common = intrsect(names_a, names_b, flag);

/*
** Notice the $ in front of 'names_common'
** tells GAUSS to print as character data
*/
print $names_common;

The code above, will print the following output:

cocoa
  oil

Remarks

  1. If not matches are found, intrsect() will return a scalar error code that can be tested for with scalmiss().

  2. Place smaller vector first for fastest operation.

  3. If there are a lot of duplicates within a vector, it is faster to remove them with the function unique() before calling intrsect().

Source

intrsect.src

See also

Functions intrsectsa()