ismember

Purpose

Checks whether each element of a matrix or string array matches any element from a separate symbol.

Format

mask = ismember(haystack, needles)
Parameters:
  • haystack (matrix) – multi-dimensional array or string array in which to search.

  • needles (vector or string array) – the elements to search for in haystack.

Returns:

mask (matrix) – multi-dimensional array or string array the same size as the input haystack, containing only 1’s and 0’s. Each element of the output, mask, will contain a 1 if the corresponding element of haystack matches one or more elements in needles.

Examples

Basic numeric example

haystack = { 105 120,
              89 141,
             120 180 };

needles = { 105, 180 };

mask = ismember(haystack, needles);

After the above code, mask will equal:

1 0
0 0
0 1

Find instances of bad values in a string array

// Set vector to search in
names = "GDP" $| "NaN" $| "." $| "Inflation";

// Set values to search for
missing = "NaN" $| "" $| ".";

/*
** Search 'names' for instances of any of
** the strings in 'missing'
*/
mask = ismember(names, missing);

After the code above, mask will equal:
0
1
1
0

Remarks

ismember() is similar to the dot operators .== and .$==. The difference is that ismember() allows for the comparison of more than one needle in each element of the haystack.

See also

Functions indexcat(), indnv(), contains(), rowcontains()