主要内容

iscategory

确定输入是否为类别的名称

说明

tf = iscategory(A,catnames) 将返回一个包含逻辑值 1 (true) 的数组,其中 catnames 中的数据为 A 类别。否则,iscategory 将返回逻辑值 0 (false)。

tf 的大小与 catnames 相同。

示例

示例

全部折叠

创建一个分类数组。

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

A 的类别是衣物的名称。它们来自输入数组的唯一值。

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

确定衣物的名称 shirtpantssocksshoes 是否属于 A 的类别。

catnames = ["shirt" "pants" "socks" "shoes"]
catnames = 1×4 string
    "shirt"    "pants"    "socks"    "shoes"

tf = iscategory(A,catnames)
tf = 1×4 logical array

   1   1   0   0

shirtpantsA 类别,但 socksshoes 不是。

iscategory 没有提供有关类别 hat 的任何信息,因为它没有包含在 catnames 中。

创建一个分类数组。

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

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

确定 boat 是否属于 A 中的类别。

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

iscategory 返回 1 (true),即使 A 中没有元素属于类别 boat 也是如此。

创建一个分类数组。

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

您可以使用pattern匹配一个或多个类别名称。例如,通过使用通配符模式来确定是否有以 Y 开头的类别名称。您可以使用 wildcardPattern 函数创建通配符模式。

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

确定是否有以 X 开头的类别名称。

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

输入参数

全部折叠

输入数组,指定为 categorical 数组。

类别名称,指定为字符串数组、字符向量、字符向量元胞数组或 pattern 数组。

扩展功能

全部展开

版本历史记录

在 R2013b 中推出