setdif

Purpose

Returns the unique elements in one vector that are not present in a second vector.

Format

y = setdif(v1, v2[, typ])
Parameters:
  • v1 (Nx1 vector) – data

  • v2 (Mx1 vector) – data

  • typ (scalar) –

    Optional input, type of data.

    0

    character, case sensitive.

    1

    numeric (Default).

    2

    character, case insensitive.

Returns:

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

Examples

Basic example

// Create a vector of years
y1 = { 1980,
       1984,
       1988,
       1992,
       1996,
       2000,
       2004,
       2008,
       2012,
       2016 };

y2 = { 1980,
       1988,
       1992,
       2000,
       2008,
       2016 };

// Set 'y_diff' equal to years in 'y1' and NOT in 'y2'
y_diff = setdif(y1, y2);

After the code above, y_diff will be equal to:

1984
1996
2004
2012

Character data

// Create 2 vectors of character data using the
// numeric concatenation operator
sp500 = "aapl" | "goog" | "msft" | "xom" | "wfc" | "jnj";
nasdaq = "aapl" | "msft" | "amzn" | "goog" | "fb" | "gild";

// Set type to 'character, case insensitive'
typ = 2;

// Find characters in sp500 and NOT in nasdaq
sp_only = setdif(sp500, nasdaq, 2);

// NOTE: The $ in front of the variable name tell
// GAUSS to print the variable as character data
print $sp_only;

The code above will produce the following output:

jnj
wfc
xom

Remarks

Place smaller vector first for fastest operation.

When there are a lot of duplicates, it is faster to remove them first with unique() before calling this function.

Source

setdif.src

See also

Functions setdifsa(), union()