Main Content

iscategory

Determine if inputs are names of categories

Description

tf = iscategory(A,catnames) returns an array containing logical 1 (true) where the data in catnames is a category of A. Otherwise, iscategory returns logical 0 (false).

tf is the same size as catnames.

example

Examples

collapse all

Create a categorical array.

A = categorical(["shirt" "pants"; "pants" "hat"; "shirt" "pants"])
A = 3x2 categorical
     shirt      pants 
     pants      hat   
     shirt      pants 

The categories of A are names of articles of clothing. They come from the unique values of the input array.

categories(A)
ans = 3x1 cell
    {'hat'  }
    {'pants'}
    {'shirt'}

Determine if the names of articles of clothing, shirt, pants, socks, and shoes, are categories of A.

catnames = ["shirt" "pants" "socks" "shoes"]
catnames = 1x4 string
    "shirt"    "pants"    "socks"    "shoes"

tf = iscategory(A,catnames)
tf = 1x4 logical array

   1   1   0   0

shirt and pants are categories of A, but socks and shoes are not.

iscategory does not tell us anything about the category, hat, because it was not included in catnames.

Create a categorical array.

data = ["plane" "car" "train" "car" "plane"];
categoriesOfData = ["boat" "car" "plane" "train"];
A = categorical(data,categoriesOfData)
A = 1x5 categorical
     plane      car      train      car      plane 

categories(A)
ans = 4x1 cell
    {'boat' }
    {'car'  }
    {'plane'}
    {'train'}

Determine if boat is a category in A.

tf = iscategory(A,"boat")
tf = logical
   1

iscategory returns 1 (true), even though no element of A belongs to the category boat.

Create a categorical array.

C = categorical(["Y" "Yes" "Yeah" "N" "No" "Nope"])
C = 1x6 categorical
     Y      Yes      Yeah      N      No      Nope 

You can match one or more category names by using a pattern. For example, determine if any category names start with a Y by using a wildcard pattern. You can create a wildcard pattern with the wildcardPattern function.

tf = iscategory(C,"Y" + wildcardPattern)
tf = logical
   1

Determine if any category names start with an X.

tf = iscategory(C,"X" + wildcardPattern)
tf = logical
   0

Input Arguments

collapse all

Input array, specified as a categorical array.

Category names, specified as a string array, character vector, cell array of character vectors, or pattern array.

Extended Capabilities

Version History

Introduced in R2013b