categorical.ismember
categorical: TF = ismember (A, B)
categorical: TF = ismember (A, B, 'rows')
categorical: [TF, index] = ismember (…)
categorical: [TF, index] = ismember (…, 'legacy')
Find categorical elements in a set.
TF = ismember (A, B) returns a logical array
TF of the same size as A containing true for each
corresponding element of A that is in B and false
otherwise. <undefined> elements are not equal with each other
and always return false. If A and B are ordinal,
they must have the same ordered set of categories. If neither A
nor B are ordinal, then this restriction is relaxed and comparison
is performed using the category names. Comparison between an ordinal and
an unordered categorical array is not allowed. Either A or B
may also be a string array, a character vector, or a cell array of
character vectors containing one or multiple category names to compare
against an unordered categorical array.
TF = ismember (A, B, only
applies to categorical matrices with the same number of columns, in which
case the logical vector TF contains 'rows')true for each row of
A that is also a row in B. TF has the same number of
rows as A.
[TF, index] = ismember (A, B) also returns
an index array of the same size as A containing the lowest index in
B for each element of A that is a member of B and 0
otherwise. If the 'rows' optional argument is used, then the
returning index is a column vector with the same rows as A and it
contains the lowest index in B for each row of A that is a
member of B and 0 otherwise. If the 'legacy' optional
argument is specified, then the highest index of matched elements is
returned. Unless multiple matches exist, the 'legacy' option has
no effect on the returned index.
Source Code: categorical
ismember tests, element by element, whether each value appears in a second set given as category names or as another categorical array.
C = categorical ({'M'; 'S'; 'XL'; 'L'})
C =
4x1 categorical array
M
S
XL
L
ismember (C, {'S', 'M'})
ans = 1 1 0 0